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!

Kyle Eslick is WordPress enthusiast who took his passion for WordPress to the next level in 2007 by launching WPHacks.com as a place to share hacks, tutorials, etc. Follow Kyle on Twitter @KyleEslick!

  1. Ian Blackford says:

    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


  2. John Hoff - eVentureBiz says:

    If only everything were that simple 😉

  3. Tim Norton says:

    Thanks Kyle, this is simple and easy.

  4. Kyle Eslick says:

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

  5. Luke says:

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


  6. Kolia Shlapak says:

    HackWordpress rulezz!

  7. Missy says:

    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.


  8. Michiel says:

    What about simply using this code:

    Your code here…

  9. Michiel says:

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

    your code here

    What about this?

  10. Michiel says:

    Well… that’s working great… 🙂


  11. james says:

    nice hacks..
    keep it up.

  12. ET says:

    thanks a lot for that, its such a simple snippet of code and very simple instructions 🙂

    you are a living legend!

    see ya

  13. Boon says:

    Thanks for this! Easy hack…. if you want after the 3rd article for example, change the 1 to a 3… the number represents the article # that you want to place the ad after.

    Make sure to add tags before/after the ad to give it some space. You can see the end result on my blog.

  14. Jason H says:

    Great code. Is there any way to make this only take effect after the first post on only the first page?

  15. Jason H says:

    Never mind. I figured it out. I put ‘& !is_paged()’ in the conditional to get it on the homepage only and not its subsequent pages or ‘previous entries’.

  16. Dana says:

    Thanks for enlightenment!

  17. Mike Online says:

    Super! nice hacks..
    keep it up.

  18. Mike says:


    I been trying to figure this out for a while and you solve the problem with a couple of lines of code.

    Well done, and thanks for the tip.

  19. Kal-El says:

    Here is another way : http://www.saijogeorge.com/2009/12/24/ads-after-the-first-post-in-wordpress-on-homepage.html

    With that you can add it after nay post not just the first.

  20. aan says:

    It’s cool tutorial and easy to be implemented.
    Can you help me? How to add it on after first post and then add it again on 3 next post.

  21. Fabio says:

    Thanks! This was exacly what I needed. Simple and easy!

  22. Piseth says:

    I try to implement this code but it appears at the bottom of the post after the last post on homepage. Hope can get it works as everyone else

  23. blackhumor says:

    Very nice and easy hack…

    I wonder why didn’t anyone make a small plug in for this, so we don’t have to fix it any time we change template…

  24. Richard says:

    What happends if you are running a plugin like Datafeedr Random Ad to manage your ads, what would the code look like?

  25. bobby says:

    how about blogspot, is there anything that can help??thanks.

  26. shazy says:

    Nice But how to do this in blogger

  27. MiriLaiLai says:

    thanks mate, it helped a lot !

  28. Edille says:


    Thanks for the code 🙂 Have hard time looking for this.

    Thanks again.

  29. Use Wp-Insert.. The plugin lets you pick the posts you want to show ads on (1st post, 2nd post etc)

  30. Tom Durkin says:

    Cleanest way of doing it I can find…awesome, thanks!

Trackbacks/Pingbacks »

  1. Sxc.dk » Mere om WP says:
  2. WordPress ?? ??? ??? snippet says:
  3. 100+ Killer Wordpress Resources | Steffan Antonas’ Blog says:
  4. Grumpy Git . org » Blog Archive » Links for 2009-05-03 [del.icio.us] says:
  5. 6 Tips to Take Your WordPress Theme from Good to Great - Fantastic Web Design says:
  6. Show | Display | Insert ad after first post in WordPress | Khapay says:
  7. Show | Display | Insert ad after first post in WordPress says:

Tweetbacks »