How To: Insert Horizontal Banner between 1st and 2nd Post

With the help of Edrei and sheer luck by using one of the Wordpress codes as a search phrase, I finally found how to insert a banner advertisement in between the 1st and 2nd post. I’ve been always pondering about this and because I’m no PHP whizz, now I’ve ridden myself of one of my Wordpress mysteries. Sorry fellows, but this is only for the Wordpress users.

In early 2006, Darren of Problogger initially released a tutorial for this but at the last comment someone said that it wasn’t supported on version 2.x and only version 1.5.

Though the direction Edrei pointed me to was correct, it looked like it couldn’t fit into the code Simpla theme was using. That’s when I thankfully stumbled upon a thread on the WickedFire forum. In the thread, a member discussed how they managed to include an Adsense banner only in the 1st and 2nd post which was exactly what I was looking for. Their method was to edit the header and index template.

But further down the thread, a different member shared some knowledge for obtaining the same results using only the Wordpress Index template which was great. Less code to edit and even less files to touch. The other great part about this code is that you can even display a banner after the 10th post even!

The method I’d like to share with everyone based on the forum posting:

<?php $count = 0 ?>
place above –> <? php if (have_posts()) : ?>
<?php if ( $count == 0 ) : ?>
<?php include (TEMPLATEPATH.’/ad2.php’) ?>
<?php endif // $count == 0?>
<?php if ( $count == 1 ) : ?>
<?php include (TEMPLATEPATH.’/ad3.php’) ?>
<?php endif // ( $count == 1 ) ?>
<?php $count++ ?>
place above –> <? php endwhile; ?>

It looks confusing but don’t worry I was confused as well until after I experimented with it and I’m going to share my end results with you so you won’t have to experiment again - I hope.

You will have to place the code:

<?php $count = 0 ?>

above the..

<?php if (have_posts()) : ?>

then after the..

<?php comments_template(); ?>

place this code under it..

&lt?php if ( $count == 0 ) : ?>
Replace this line with Advertising code
&lt?php endif // $count == 0 ?>
&lt?php $count++ ?>

but remember that code has to be above..

<?php endwhile; ?>

which is normally at the bottom.

Now like I said I’m no PHP wizard but I’m going to try explaining to you what’s going on. The first part of the PHP code is to setup a custom counter. Then the $count == 0 is to initiate the counter as well as telling Wordpress to place your banner after the first post (represented by the value 0). That means if you changed the 0 to 1, your banner will most likely later end up below the 2nd post.

I don’t really know what’s the endwhile property about but my guess is that it’s to stop the current loop of Wordpress to capture the number of recent posts normally set in your Wordpress settings.

So that’s it, give it a try and remember that this is for version 2.x.

Or would you please share your cleaner and meaner alternative method here?

Other articles you should have read:

  • My Google Adsense is Like Investing in Unit Trust
  • Website Strategy: Best Navigation Menu Positions
  • Sony Ericsson K750
  • Earn Money From Your Favorite Forum
  • A ‘Standard’ Website
  • Comments

    12 Responses to “How To: Insert Horizontal Banner between 1st and 2nd Post”

    Pages: « 1 [2] Show All

    1. Response #1 by thamkengyew on September 21st, 2008

      this helped .. well the conversation between you guys helped the most ha ha.
      Thanks anyway guys

    2. Response #2 by Danny Foo on September 21st, 2008

      Thanks for informing me about the code problem, tham. I never knew it’s not displaying properly even with <code> implemented. :(

    Pages: « 1 [2] Show All


    Leave a Reply