300x250 How To: Insert Ads Only After the First Post

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!
  • Print This
  • 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. Connect with Kyle on Twitter or Google+!

    There Are 30 Responses So Far »

    • Pingback: Sxc.dk » Mere om WP

    • Ian Blackford

      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

    • Pingback: WordPress ?? ??? ??? snippet

    • Pingback: How To: come inserire adsense dopo il primo articolo su Wordpress! - Geekissimo

    • John Hoff – eVentureBiz

      If only everything were that simple ;)

    • http://mothertheme.com Tim Norton

      Thanks Kyle, this is simple and easy.

    • http://kyleeslick.com Kyle Eslick

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

    • http://www.blogaboutbeer.com Luke

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

      Thanks!

    • Pingback: 100+ Killer Wordpress Resources | Steffan Antonas’ Blog

    • Kolia Shlapak

      HackWordpress rulezz!

    • http://www.bloggerssearch.com Missy

      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.

    • Michiel

      What about simply using this code:

      Your code here…

    • Michiel

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

      your code here

      What about this?

    • Michiel

      Well… that’s working great… :)

      Sorry!

    • james

      nice hacks..
      keep it up.

    • http://tariksport.com/nfl-blog/ ET

      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/

    • Boon

      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.

    • Pingback: Grumpy Git . org » Blog Archive » Links for 2009-05-03 [del.icio.us]

    • http://www.404techsupport.com Jason H

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

    • http://www.404techsupport.com Jason H

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

    • Dana

      Thanks for enlightenment!

    • http://www.middle-east-pages.com/ Mike Online

      Super! nice hacks..
      keep it up.

    • Mike

      Whoa!!

      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.

    • Pingback: 6 Tips to Take Your WordPress Theme from Good to Great - Fantastic Web Design

    • Kal-El

      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.

    • http://www.ansbook.net/ aan

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

    • Fabio

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

    • http://piseth.info Piseth

      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

      • http://www.peterlehto.com Peter Lehto

        The same happens with me… otherwise It works great!

        Thanks!

    • http://www.blackhumor.net blackhumor

      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…

    • Pingback: Show | Display | Insert ad after first post in WordPress | Khapay

    • http://www.grillingwithrich.com Richard

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

    • bobby

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

    • Pingback: Como mostrar Adsense después del primer post en el home Wordpress

    • shazy

      Nice But how to do this in blogger

    • MiriLaiLai

      thanks mate, it helped a lot !

    • Pingback: Show | Display | Insert ad after first post in WordPress

    • Edille

      Hi,

      Thanks for the code :) Have hard time looking for this.

      Thanks again.

    • http://smartlogix.co.in Namith Jawahar

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