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

Variations…

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

No related posts.

81 Comments…

 Share your views
  1. thanks a lot dear !

    i was eagerly waiting for this post…

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

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

      @Joe
      I thank you Joe for your original hack and accepting this article in sporty manner. :-)

  2. Worth a Digg :D
    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 :D

  3. Thanks for the plug Rahul. Great tip and instructions.

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

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

  6. Thanks Rahul, would be contacting you in a day or so :D

  7. @Keith
    Yep you are right. :-)
    But I prefer direct coding as to keep my plugin list as small as possible.

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

    @Gaurav
    Your welcome buddy. :-)

  8. how to get adsense code.

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

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

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

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

    Can you help me with that

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

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

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

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

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

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

  19. great tip for starters!! I have something very similar in my website, adsense blog after every post :)

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

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

  22. 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:(

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

    here is my index.php along with ad code,teenstarsworld.com/demo/plz.zip

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

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

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

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

    • @t.j detwiler
      Link is broken: teenstarsworld.com/demo/plz.zip

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

      @Sean
      U can put anything instead of Adsense code. :-)

  26. 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! :)

  27. really useful tips..thanks a lot!

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

    Thanks
    Amy

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

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

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

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

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

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

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

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

  37. THANKS for the tips! help a lots!!!

  38. [...] Related: Show AdSense ads after each post in blogger | wordpress blog [...]

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

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

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

  42. 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 :)

  43. Hi,

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

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

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

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

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

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

  49. Can you tell me how to do this with blogger

  50. 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
    http://wordpress.org/extend/plugins/best-google-adsense/

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

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

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

  54. this code

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

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

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

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

  58. Oh, I just found it.. lol..

    <?php if ($adcount

  59. 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() );
    endwhile;
    else :
    ?>

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

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

Leave a Comment

  • Facebook
  • GitHub
  • Twitter
  • LinkedIn
  • Google

Your email address will not be published.

*