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?

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

  1. Hmmm, it seems this doesn’t work with k2 theme? I’ve no idea how to edit the index files for k2 theme.

    The files is different from regular theme.

  2. I downloaded the K2 theme to testand found the problem

    In the K2 theme, when you view the Main Index Template in the Theme Editor you’ll notice that you don’t see the there.

    For the K2 theme, they’re using another file to pull in the blog entry data; Page Template.

    However, I must worn you too that the PHP in there is different in comparison with the Simpla theme. So you might need to do some trial and error as well later.

    But let us know how it goes. :)

  3. In k2 theme there’s a file called the loop. you may put danny Foo’s code there below “Normal Entries”. CMIIW

  4. Izzatz, in your Theme Editor click the theloop.php file.

    In there, search for the part where it says /* Start the loop */

    Below that is the PHP to generate your ‘normal entries’.

    I admit that you have to be a little familiar with WordPress or PHP to edit this.

    Anyway, tell us how it goes. :)

  5. Nice going!

    So taking it one step further, how would one modify the above to insert adsense code very “x” number of posts?

  6. The thing I learned about the code is the part it mentions ..if ( $count == 1 ).. actually starts the post count number. 0 being the 1st post and 1 being the 2nd post.

    Which means that if you want it to show on the 10th post, I’m assuming the number you’re looking for is 9.

    But I;m no PHP whiz so I’m playing on a hunch.

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

Leave a comment