Show AdSense Ads After First or Every Post in WordPress Blogs [How-To]

Long time back, I wrote about showing Google AdSense ads after each post in blogs. Now I am doing exactly opposite by limiting ads to only first post on home, archives, categories pages. Johnson wants to know how I am doing it!

To do this, You need to edit famous wordpress loop. Such a loop can be found in your current wordpress theme’s index.php. There may be other files present in your theme directory which uses loop like archives.php. They may need to be updated as well.

Listed below are steps to edit loop in index.php file. Similar steps can be performed for other files too, if required.

1. Go to WordPress Dashboard >>Design >> Theme Editor.

2. Click on index.php or Main Index Template file. It will open in Theme Editor on left.

3. Find a line:

<?php while (have_posts()) : the_post(); ?>

4. Add before it:

 <?php $adcount = 1; ?>

5. So final code will look:

<?php $adcount = 1; ?>

<?php while (have_posts()) : the_post(); ?>

6. Now find:

<?php endwhile; ?>

7. Add before it:

<?php $adcount++; ?>

8. So final code will look:

<?php $adcount++; ?>

<?php endwhile; ?>

9. Now inside loop, i.e. after “while” line but before “endwhile” line put your AdSense codes. Exact location depend on your choice between ads before post, ads after post, etc. Important thing is you have to put your AdSense code in a if-block. below is an example…

<?php if ($adcount == 1) : ?>

<!-- Put Your AdSense Code here --> 

<?php endif; $adcount++; ?>

That’s it. Save changes and check if it works.


1. If you want to put ads after first two post…

In step 9 code, Change $adcount == 1 to $adcount == 2

2. If you want to put ads after first three post…

In step 9 code, Change $adcount == 1 to $adcount == 3

3. If you want to put ads after each post…

In step 9 code, Change $adcount == 1 to $adcount == 3. Yes this is same like above. Because AdSense only allows maximum three ad-units on a a single page. This way you avoid blank space created after each post when you directly put AdSense code after each post.

If you need any help, or stuck at some point, feel free to contact via comment.

(Credit: Based on Joe Hayes post.)

Author: Rahul Bansal

I am a geek (wordpress), blogger (tech) & entrepreneur (rtCamp)

  • thanks a lot dear !

    i was eagerly waiting for this post…

    • @Johnson
      Ya, I know it. Sorry for delay.

      Buddy don’t worry about moving to wordpress. I am really good at wordpress now so I will be there to handle technical issues for you.

      I thank you Joe for your original hack and accepting this article in sporty manner. 🙂

  • Worth a Digg 😀
    I had tried similar thing on blogspot but to no avail, there wasn’t a way to show ads only after first post. But this is surely going to come handy when I start on WP 😀

  • Joe

    Thanks for the plug Rahul. Great tip and instructions.

  • Nice tutorial, though there are several plugins that allow you to do this, I can recollect adsense injection being one of them

  • That’s a nice tip. I actually knew it, but not in a right procedural way. All blogs at hyperwebenable will be added with such codes so ads appear only on first post’s top and bottom.

  • Thanks Rahul, would be contacting you in a day or so 😀

  • @Keith
    Yep you are right. 🙂
    But I prefer direct coding as to keep my plugin list as small as possible.

    Yep that variation can also achieved by setting counter to 1 and putting codes at two places.

    Your welcome buddy. 🙂

  • aju

    how to get adsense code.

  • aju

    thats wat. for google adsense or sum other adsense wre shud i go andd sign up. can u give me the link pls.

  • Realy great explanation! Thanks a lot. At first it wouldn’t work because I already changed the number at stpe 4 :_)

  • Thanks!
    I prefer to code these things in as well. Your guide was nice and easy to follow and I’m using it with one of my sites now.

  • Sebastian

    How to display this ad after 1 post but ONLY ON HOME PAGE not on older entries

    Can you help me with that

  • Peter Pan

    Hi, I’ve added the ad below the first post only, but I would like to center the entire ad block… can someone please help? thanks.

    • @Peter Pan
      I am not sure. But try adding the tag

      before the ad, and
      at the end of the ad.

    • @Peter Pan
      Just surround adsense codes with a DIV or P tag with attribute align=”center”

  • Peter Pan

    @Deepak Jain – hi, what tag? sorry i dont know anything about html…thx

  • What if you want to have an add come up every three post. Not three posts consecutively. I want my long banner ad to come up every other three posts.

    • @Troy Steuwer
      Yep… It can be done! 🙂
      In above code, in step 9

      replace line:
      < ?php if ($adcount == 1) : ?>

      with line:
      < ?php if ( $adcount % 3 == 1 ) :?>

  • @Rahul Bansal – You rock my world. Thanks!

  • @Rahul Bansal – Hey, FYI, all of your code and instructions don’t work with 2.7 🙁 Can you update it?

  • oops, nvm, wrong platform 🙁

  • Wait, no it’s not. This is for WordPress and not Blogger, correct? I tried doing exactly what this page says to do in WordPress 2.7. It doesn’t work, in fact, it just crashes the page. Can you update it for 2.7?

  • Got it to work, nvm. Sorry for so many posts ^_^

  • great tip for starters!! I have something very similar in my website, adsense blog after every post 🙂

  • Thanks for the post, I tweaked your example so I could add individual ads to each post on the index page (more ads than just Adsense). Following your instructions, use the following in step 9 to specify ads in the first four posts:

    If you use WPads, you’ll understand how to add more “ad spaces”. Then you just need to add more elseif statements to show the ads.

    • @Colby
      Can you repost codes with HTML entities escaped.
      Wordpress stripped out all code changes form your comments. 🙁

  • Tom

    Great :), I was searching for a way to do this. It was easy to implement.

  • hi i add this code to my self hosted wp blog but only showing ads in 1st post ,m alredy changed 1 to 3,plz help me:(

    • @t.j detwiler
      Crosscheck each step again. This works well for sure! 🙂

  • plz chk my index.php and add the code,m unable to do it.

    here is my index.php along with ad code,

  • can u plz tell me what i m doing wrong herer is the code

    <a href=”” rel=”bookmark” title=”Permanent Link to “>

    <a href=”” >read user’s comments ()
    <img src=”/images/spacer.gif” alt=”” width=”1″ height=”30″ />

    Not Found
    Sorry, but you are looking for something that isn’t here.

  • plz somebody help me

  • Does this code only work with adsense, can I use it with any code.
    for example if I want to use my own ads instead do I need to do anything special?

    • Rahul Bansal

      @t.j detwiler
      Link is broken:

      R u sure that you have posted right code in comments?
      Please check ur comment as I think your codes has been stripped by WordPress.

      U can put anything instead of Adsense code. 🙂

  • Hiya, i have seen your site when searching a few weeks ago and i really love the design! I just bought a new 3 character domain (cost me a packet) for a niche review blog, and i was wondering if your design is a free or paid one? I’m new to WordPress and about to set it up, and i would really like to get something with a similar look to yours. Any ideas where i could download or buy something similar? Thanks for your help! 🙂

  • really useful tips..thanks a lot!

  • hey thx for the info…it was great…can u please tell that whether the text of the adsense by following this method, would automatically be the same as that of the original post. cuz i want it to be in verdana


    • Rahul Bansal

      You can control ads font from AdSense panel.

  • Thanks for the tutorials on inserting adsense in your wordpress..this was really helpful now.maybe i can actually make some money with it

    • Rahul Bansal

      Welcome To Devils Workshop! 🙂

  • Thanks for the handy tutorial Rahul! I’ve been looking to insert ad images after the first *and* fourth posts, and your code helped out. Cheers.

  • Thanks for the tip i was searching plugin for it.

  • I am trying to get my content to show up after the second post only. Mine keeps showing up after the first post, I am using the following code, plus the other code you provided.

    Sponsored by

    Any help would be greatly appreciated. Thank you!

  • nishant

    i am able to see the ads under the post on the homepage but when i open the post the ad does not appear

    please help!!

  • @Nishant: If you want the ads to show up when you open the post, you’ll need to add the code to single.php.

  • semafori

    nice post, but i have a problem, ok here it is help me please, i am new to this wordpress php things here is the problem.
    i only get first post 1 add, or i can put it on the second or third,
    what i cant do is that i like to show add on first 3 posts
    so help me please i am using wordpress 2.8.5.

    thank you

  • This is the best blog i have ever seen. great work! Thanks

  • Pingback: Different Ways Of Implementing Adsense On Your Blog | CSS Collections | Masterful CSS Collections()

  • f-k

    THANKS for the tips! help a lots!!!

  • Pingback: Google AdSense Video Unit Available in India | Devils Workshop()

  • Christina

    Old post or not, this works great!! Thanks so much. I’m using it to allow my related content to show an ad on every other post. Perfect!!

    Just a sidenote, I’m using this on WPMU. (Just thought I’d throw that in there for anyone using WPMU and searching for something that will actually work)
    Thanks again!!!

    • @Christina : Glad this post was help. Will update it regarding the WPMU info. Thanks.

  • prashant

    i want to do the same with smowtion ads…can i do the same with this code???

  • Thanks for your help! 🙂

  • Thanks a lot for another marvellous article. I am always on the look-out for original WordPress tips to suggest to my own readers. Thank you for posting this tutorial . It’s just what I was searching for. Truly marvellous post.

  • Thanks for this, I chose to stick an ad after the first and second post (I have 3 posts displayed at anyone time) and a big square ad in the upper right corner, gonna see how my CTR is 🙂

  • Greg


    This doesn’t seem to be working properly on the latest version. The ads only appear on the first post.

  • Great and very helpful post. I want to use this type of set-up to to add static advertising/images between posts and want to know if there’s a way to embed different images between posts. For example I want to use Image A between posts 2 and 3 and Image B between posts 4 and 5. Is there a way to do that?

  • arya

    u rox
    was searching dis thing …frm many dayzz …
    thnk u so much …^_^

  • Dan

    Thanks very much for this…great help. With a slight modification I was able to get ads to show up on the first 3 posts (only) on my index page.

    <?php if ($adcount

    Thanks again!

  • Hi!! i used the code and did follow each step but when i change the code to 3 or 2 so that the ad will be shown each 3 or 2 posts it wont work. it still keeps showin after every single posts. any idea???

  • Rahul Mehta

    I want the adsense ads to be displayed on all my wordpress posts.I have close to 100 posts and I want the adsense ads to be displayed on all my adsense,this will only make the ads available for only one or two posts

  • Can you tell me how to do this with blogger

  • I do not like to copy and paste adsense codes. So, I use plugins at my blog. The plugin I use is Best Google Adsense

  • Thanks a lot. After a few days digging the net i’ve finaly found your post and solve my problem. Pace.

  • PunK

    Hey ! I want to use ads after 2nd & 4th post
    $adcount == 2 || $adcount == 4
    so hw to implement this ?

    • I too have the same requirement. I need to show after 2nd and 4th posts. Is this code still working for latest WP? Thanks.

  • yash

    hi friend i am using this code

    <?php dt_show_ads('position=Archives&before=&after=&title=्&#2334');?>

    i want to show ads after every 4th post in index.php

  • yash

    this code

    <?php dt_show_ads('position=Archives&before=&after=&title=्&#2334′);?>

  • instead of adsense i want to add clear tag for each after 2 posts. how can write? help me.

  • Worked like a charm…I am using the counter until it reaches 5 and then display a banner ad…That’s exactly after half of the articles from a page.

  • Anonymous

    Hi, I want to show ads after each post for first three posts..

  • Anonymous

    Oh, I just found it.. lol..

    <?php if ($adcount

  • I want to add sharing icons below every post . How can I do that?

    Also my code looks like this

    $swift_slider_posts_ids, ‘paged’ => $paged ) );

    if ( have_posts() ) :
    /* Start the Loop */

    while ( have_posts() ) : the_post();
    get_template_part( $layout, ‘blog-‘.get_post_format() );
    else :

  • hi, when i insert code it just comes up –> do you know what I am doing wrong? Thanks!

  • Luis

    hi, =)

    i was trying to use adsense in the 2 first posts but it doesnt show…
    i’ve done some tries and it works if i use the same number on step 4 and 9 in this line and it only shows ads in first post… :/