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?


Have something to say? Leave a comment

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

Pages: [1] 2 » Show All

  1. Response #1 by Izzatz - xiii on January 22nd, 2007

    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. Response #2 by Danny Foo on January 22nd, 2007

    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 < ? php if have posts ?> 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. Response #3 by buchin on January 28th, 2007

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

  4. Response #4 by Izzatz - xiii on January 28th, 2007

    im still lost here.. where exatcly did u mean ‘normal entries’ is? thx for d lighten up 🙂

  5. Response #5 by Danny Foo on January 31st, 2007

    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. 🙂

  6. Response #6 by gmo on February 3rd, 2007

    Nice going!

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

  7. Response #7 by Danny Foo on February 4th, 2007

    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.

  8. Response #8 by Radon on February 22nd, 2007

    Thank you so much. I’m in the process of migrating my site ( to WordPress (development here I’ve wanted to be able to put an ad between posts FOREVER and now with WordPress and people like you, I can do it. I’m very grateful.

    Works like a charm.

  9. Response #9 by My Google Adsense is Like Investing in Unit Trust on February 8th, 2008

    […] blogging. It was during this period I moved from the Movable Type blogging platform to the WordPress publishing platform. So revenue was lost as I continued blogging without having a good idea on how […]

  10. Response #10 by Google Success on April 6th, 2008

    Thank you so much for the tips. The mod works very well.

Pages: [1] 2 » Show All

Leave a Reply

Other articles you should have read: