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

Related posts:

Comment RSS · TrackBack URI

49 Comments (including Pingbacks/Trackbacks) so far »

  1. #
    Johnson on July 10, 2008

    thanks a lot dear !

    i was eagerly waiting for this post…

  2. #
    Gaurav on July 10, 2008

    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. #
    Joe on July 10, 2008

    Thanks for the plug Rahul. Great tip and instructions.

  4. #
    Rahul Bansal on July 10, 2008

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

  5. #
    Keith Dsouza on July 10, 2008

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

  6. #
    Pavan Kumar on July 11, 2008

    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.

  7. #
    Gaurav on July 11, 2008

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

  8. #
    Rahul Bansal on July 14, 2008

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

  9. #
    aju on July 27, 2008

    how to get adsense code.

  10. #
    Rahul Bansal on July 27, 2008

    @aju
    Signup for adsense… :-)

  11. #
    aju on July 28, 2008

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

  12. #
    Rahul Bansal on July 31, 2008
  13. #
    Zoekmachine Marketing on August 26, 2008

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

  14. #
    Geek on September 15, 2008

    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.

  15. #
    Rahul Bansal on September 18, 2008

    @Geek
    Your welcome buddy… :-)

  16. #
    Sebastian on October 30, 2008

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

    Can you help me with that

  17. #
    Peter Pan on November 8, 2008

    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.

  18. #
    Deepak Jain on November 8, 2008

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

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

  19. #
    Peter Pan on November 8, 2008

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

  20. #
    Deepak Jain on November 10, 2008

    @Peter Pan -
    center tag.

  21. #
    Rahul Bansal on November 25, 2008

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

  22. #
    Troy Steuwer on November 29, 2008

    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.

  23. #
    Rahul Bansal on December 1, 2008

    @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 ) :?>

  24. #
    Troy Steuwer on December 7, 2008

    @Rahul Bansal - You rock my world. Thanks!

  25. #
    Troy Steuwer on December 7, 2008

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

  26. #
    Troy Steuwer on December 7, 2008

    oops, nvm, wrong platform :(

  27. #
    Troy Steuwer on December 7, 2008

    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?

  28. #
    Troy Steuwer on December 7, 2008

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

  29. #
    Raju on December 7, 2008

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

  30. #
    Rahul Bansal on December 8, 2008

    @Troy Steuwer -
    Its ok… :-) Btw thanks for confirming above code to be working with Wordpress 2.7. :-)

  31. #
    Colby on January 1, 2009

    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.

  32. #
    Rahul Bansal on January 3, 2009

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

  33. #
    Tom on January 6, 2009

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

  34. #
    t.j detwiler on January 14, 2009

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

  35. #
    Rahul Bansal on January 15, 2009

    @t.j detwiler
    Crosscheck each step again. This works well for sure! :-)

  36. #
    t.j detwiler on January 15, 2009

    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

  37. #
    kapil on January 17, 2009

    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.

  38. #
    t.j detwiler on January 18, 2009

    plz somebody help me

  39. #
    Sean Brown on March 16, 2009

    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?

  40. #
    Rahul Bansal on March 19, 2009

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

  41. #
    Max the Micro Niche Finder on March 24, 2009

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

  42. #
    Rahul Bansal on March 29, 2009

    @Max
    Sorry for late reply buddy.
    We provide theme design service for our clients.
    Details are here. :-)

  43. #
    reez on May 7, 2009

    really useful tips..thanks a lot!

  44. #
    Write a Writing on May 18, 2009

    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

  45. #
    Rahul Bansal on May 20, 2009

    @Amy
    You can control ads font from AdSense panel.

  46. #
    RLShawdotcom on June 1, 2009

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

  47. #
    Rahul Bansal on June 2, 2009

    @RLShaw
    Welcome To Devils Workshop! :-)

  48. #
    onelargeprawn on June 20, 2009

    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.

Leave a Comment

 Name (Required)
 E-mail (Required)
 Website

Comment:

  OR Use forum if posting unrelated to this topic.
[Note: All comments will be moderated as per our comments policy.]

Subscribe without commenting


1 Trackbacks/Pingbacks

  1. How can I display ads between posts on wordpress? | IMBlogs on December 19th, 2008