PHP Magic Book | Free PHP Scripts, Tutorials and Downloads, PHP AtoZ Reloaded, free php tutorials, free php downloads, php scripts, PHP tips

Insert Adsense After the First Post in Your WordPress Blog

Adsense advertisment is very popular among bloggers. Many like to display ads on their homepage. Why not after the first post? Here is a very easy way to do so by using a simple php variable as a counter.

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 will display the Adsense code.

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

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
  <?php if ($count < 3) : ?>
          //Paste the Adsense code here
          <?php the_excerpt(); ?>
   <?php else : ?>
          <?php the_excerpt(); ?>
  <?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
  • Share/Save/Bookmark
Tags: ,
Posted in: Hacks, WordPress
Post's RSS » RSS 2.0
Post's Comments RSS » RSS 2.0

Related Posts


Post a Comment