In several of my blogs I have created for search engine optimization and marketing experimentation I have monetized them using the popular Google Adsense. One of Adsense’s limitations is that it is limited to only 3 ad units per page. These units need to be strategically positioned for maximum exposure and highest click through rate. The position I choose is to have a letterbox unit positioned directly under the title of individual posts. This means that a reader will have to view the ad before moving onto the content. I also see a lot of questions on how to actually get the ads onto the site.
There are many plugins available to display Google Adsense on a WordPress blog but the one I choose to use is Simple Adsense Insertion (SAI). To use this plugin you simply insert your Adsense campaign code into one of the SAI campaigns and then use the provided tags ( or “echo show_ad_camp_1();”) to position them throughout your site.
To display an ad unit under the title of each post you will need to edit your themes template:
1. Go to you WordPress Admin page.
2. Download and install Simple Adsense Insertion using the built in plugin browser.
3. Go to the plugin settings for Simple Adsense Insertion and add in your Google Adsense code and copy down the tag for your campaign “echo show_ad_camp_1();” for example.
4. You now need to modify the templates to insert your campaign onto your site. Browse to “Appearance > Editor”
5. On the right hand side of the page you need to select the Main Index Template (index.php). This is the main file for your public site so be careful not to modify the wrong thing. If you are unsure on how to backup the file off an FTP then I recommend copying all of the text from the text-area into notepad and saving it for safe keeping. If things go wrong you can always copy the text back.
6. The next step will vary slightly depending on the theme that you use and the way it outputs the posts. You need to browse through the index.php file and find where the content of a post is sent to the browser. The easiest to find this is use your browsers find function and look for “the_content(“. This should be positioned in between a <div> tag with an id of something like “content”, “entry”, or “story”. This is all theme dependant.
To display your ad unit before the post’s content simply add “<?php echo show_ad_camp_1(); ?>” before “the_content(” but after the opening <div id=”content”>.
7. You can now save the changes to the file and have a look at the result on your home page. There should be a Google Ad Unit displayed before the content of each post now.
One of the problems of adding the ad unit to each post is that Google only lets you display three units per page. If you look at posts after the top 3 you will either have a large chunk of space or a non paying ad in its place, depending on your Google AdSense settings.
To get around this problem a counter can be added to the index.php inside the loop that makes the posts. When this counter is less than 4 display a Google Ad, when it is equal or greater display nothing (without the large blank space) or display an ad from another affiliate.
Adding a counter is quite simple. Firstly find the start of the loop used to display posts. For most themes it will look something like
<?php while (have_posts()) : the_post(); ?>
Directly above this line add the following code so it looks like this:
<?php $totalPosts = 0; ?>
<?php while (have_posts()) : the_post(); ?>
To increment the totalPosts counter add the following code directly under the opening of the while loop:
<?php $totalPosts = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $totalPosts++; ?>
Now go down to where you added “<?php echo show_ad_camp_1(); ?>“. Change this to:
<?php
if( $totalPosts <= 3 )
{
echo show_ad_camp_1();
}
else
{
//add other ad campaigns here
}
?>
If you have other ads to display that use php code then simply place that code inside the else statement {}.
Save these changes and go and look at your home page now. It will no longer have blank spots or ugly unwanted ads on post four and after.


