posts

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.

Related posts

8 Comments

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*Required Fields