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; ?>
  1. So final code will look:
<?php $adcount = 1; ?>

<?php while (have_posts()) : the_post(); ?>
  1. Now find:
<?php endwhile; ?>
  1. Add before it:
<?php $adcount++; ?>
  1. So final code will look:
<?php $adcount++; ?>

<?php endwhile; ?>
  1. 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.)

83 Replies to “Show AdSense Ads After First or Every Post in WordPress Blogs [How-To]”

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

  1. 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 😀

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

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

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

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

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

    Can you help me with that

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

  8. 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?

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

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

  11. 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?

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

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

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


  14. 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!

  15. 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!!

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

  17. 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!!!

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

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

  20. Hi,

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

  21. 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?

  22. 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!

  23. 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???

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

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

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

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

  28. 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 :

  29. 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… :/

Comments are closed.