How to Display Google Adsense in First 3 Blog Posts

Google Adsense allows 3 ad strips per page as you all know. Generally, Most of the bloggers concentrate on monetizing the single pages of their blogs and leave their homepage clean without ads. If you are one of them who like ads on the Home Page like me, this will help you do it in a better way. Most of us want to show up Google ads in the first 3 posts of the blog as they grab the attention of the readers.

This can be done with a small piece of code, without using any kind of Plugins. So, In order to integrate ads efficiently into your WordPress theme. Open up your index.php file and look for the loop. The loop begins like this..

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

and this loop should end as

<?php endwhile; else: ?>
<p>Sorry, nothing matches that criteria.</p>
<?php endif; ?>

Your Google Adsense code should be placed between the start and the end of this loop. So, just pick a position (after the title or at the end of the content,where ever you want) and place the following code..

<?php if ($wp_query->current_post < 3) { ?>
<!– Google Adsense Code goes here –>
<?php } ?>

Now you have to replace the commented line ( <!– Google Adsense Code goes here –> ) with your adsense code, save this file and update your theme.  Now your theme should display three units of adsense code in your first 3 posts just like you always wanted.

Filed under: Blogging Tips
Tags: , , , ,
January 14, 2009 by: Prasanth Chandra


Leave a Reply