HOWTO – AdSense Deluxe and WordPress Template Editing


A few days ago, I posted to the Binary Bonsai list on Flickr asking about hacking the K2 theme templates for this site in order to place Google AdSense ads in controlled places – specifically above all posts and after the first or only entry on a page.

Well, after a few days poking about, I’ve figured it out and now have exactly what I want. And… I have it set up so it’s as configurable as I want it! It’s all been done with a combination of editing just one file – theloop.php – and using the AdSense-Deluxe plugin for WordPress.

Using AdSense-Deluxe you save yourself the hassle of directly inserting the Google AdSense code, and open yourself to the ability to reconfigure your ads at any time using the plugin. It’s a significantly more flexible way of doing this.

Here’s how it’s done. Note, I’m assuming you’re using the K2 theme for WordPress, but with a little thought, it should be adaptable for any WordPress theme.

Download, install and configure AdSense-Deluxe

If you’re already a WordPress user, I shouldn’t have to tell you how to do this. Follow the instructions.

Edit theloop.php

Not too tricky, but you need to pay attention.

Open theloop.php in your favorite text editor. Insert the following lines above the initialisation of The Loop. The names of the ad units are the names I use, you will need to use your own ad unit names.

< ?php // insert AdSense 5LinkUnit above everything
adsense_deluxe_ads('5LinkUnit');
?>

< ?php // initialise AdSense placement
$postnum = 1;
$showadsense = 1; // 1 = First Post
?>

This inserts your first ad unit above anything on the page except your header and initialises where you want your main ad unit; in this case, below the first post on a list page (such as the main page or an archive) or after any single-post regular page.

Then, just before the end of The Loop, above the line that reads

< ?php /* End Asides Segregation Code */ }

insert the following code.

< ?php // insert AdSense if we are at the right entry
if ($postnum == $showadsense) {
adsense_deluxe_ads('');
}
$postnum++; ?>

That’s it! That’s all there is to it. This will work for any home page, archive or single page.

8 Replies to “HOWTO – AdSense Deluxe and WordPress Template Editing”

  1. Steve, what’s the PHP code if, in addition to the “5 link unit” line on all pages, you want to have an Adsense banner on the left sidebar. I am just starting my site and don’t know anything about PHP, although I follow your editing of the loop, but I would like to have this 5 Link Ad and a left banner ad on all of my pages. An example of I site I’m trying to emulate is http://www.stevepavlina.com

    Thank you!

  2. Works great!
    I’m using this on a page with different authors which are not able / willing to insert the code in every post, so your solution is a great one for me!

    Thank you!

  3. What do you mean by “your main ad unit”? Do you mean the one defined as default trough Adsense Deluxe?

    Should the line
    adsense_deluxe_ads(”);
    in the last bit of code (to be put before the end of the loop) undergo any further editing?

    Thanks

  4. Hey,

    I just want to say thanks for this article. I’ve just started using K2 and couldn’t quite get my head around putting Adsense on my pages but thanks to you, I now have.

    Great work.

  5. One more question, what plugin you use for show this after the comment box:
    ‘Click the checkbox to subscribe to follow-up comments.”
    im looking for that too.
    Thanks

Leave a Reply