DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Many bloggers likes to add an Adsense advertisment on their homepage, after the first post. Here’s a very easy way to do it by using a simple php variable as a counter.

This seems to be a common WordPress question, but implementing it on your theme isn’t hard at all. The only thing we need is a simple php variable (here named $count) which will count how many posts are listed. If we just listed the first post, we’ll display the Adsense code.

Here’s the code. Paste it on your index.php file, instead of your current WP loop.

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
  <?php if ($count < 3) : ?>
          //Paste your Adsense code here
          <?php the_excerpt(); ?>
   <?php else : ?>
          <?php the_excerpt(); ?>
  <?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>


  1. But how to do the same in case of blogger?

  2. How can I add an IF statement so this sniplet will skip category 1?????

    Excellent code BTW 🙂


Leave a Comment

Your email address will not be published. Required fields are marked *