Have you ever tried to insert advertisements (or any bit of code really) into the WordPress post loop, then found that it will insert the banner after each post?   It really depends on what you are going for, but this usually will not be an ideal solution to placing advertisements between posts on your homepage.   If you’d like to place something only after the first post in the WordPress post loop, here is a quick hack you can do to tell WordPress to only display it after the first post.

Simply go to your themes homepage and look for the following code:

<?php endwhile; ?>

Immediately before this code, place the following code:

<?php if(!$show_ads){ ?>
Insert Code Here
<?php $show_ads = 1; } ?>

Obviously you’ll want to replace Insert Code Here with your code.  Told you it was easy!

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!
Digg This | Stumble it | Add to Del.icio.us | | Print This

There Are 16 Responses So Far. »

  1. 1 Ian Blackford
    Tuesday, August 19th, 2008 at 7:11 pm

    I need to put adverts into my post, not just in widget and the sidebar. I’ve bookmarked your page to remind me of what to do. Many thanks

    Ian

  2. 2 John Hoff - eVentureBiz
    Wednesday, August 20th, 2008 at 8:53 am

    If only everything were that simple ;)

  3. 3 Tim Norton
    Wednesday, August 20th, 2008 at 8:19 pm

    Thanks Kyle, this is simple and easy.

  4. 4 Kyle Eslick
    Friday, August 22nd, 2008 at 8:07 am

    Glad everyone found it both easy and useful! :mrgreen:

  5. 5 Luke
    Wednesday, August 27th, 2008 at 8:20 pm

    hey, is there any way to put the ad after say, the 3rd post? is it as simple as changing the to ???

    Thanks!

  6. 6 Kolia Shlapak
    Saturday, August 30th, 2008 at 2:04 pm

    HackWordpress rulezz!

  7. 7 Missy
    Saturday, August 30th, 2008 at 7:34 pm

    On a blog of mine, i use Adsense in the main index template, and 3 occurences of the banner appear on the homepage, as this is the max allowed by Google per ad unit.

    However on the 4th and remaining posts, there is an ugly space. I will try your hack and hope this helps it.

    Missy.

  8. 8 Michiel
    Sunday, August 31st, 2008 at 7:52 am

    What about simply using this code:

    Your code here…

  9. 9 Michiel
    Sunday, August 31st, 2008 at 7:57 am

    hmm… I should have known code will not be published…

    your code here

    What about this?

  10. 10 Michiel
    Sunday, August 31st, 2008 at 7:57 am

    Well… that’s working great… :)
    Sorry!

  11. 11 james
    Sunday, September 14th, 2008 at 11:43 am

    nice hacks..
    keep it up.

  12. 12 ET
    Tuesday, January 6th, 2009 at 3:14 am

    thanks a lot for that, its such a simple snippet of code and very simple instructions :-)

    you are a living legend!

    see ya
    ET
    http://tariksport.com/nfl-blog/



Leave A Comment