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

ADVERTISEMENTS

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

Share and Enjoy:
  • Digg
  • del.icio.us
  • IndianPad
  • StumbleUpon
  • Technorati
  • YahooMyWeb
  • Furl
  • Reddit
  • Google
  • TwitThis
  • Facebook
  • Slashdot
  • SphereIt
  • blogmarks
  • MisterWong

If you like this post, you may subscribe to my RSS feed or email alerts to receive automatic updates in future! Thanks for reading... :-)

Comment RSS · TrackBack URI

21 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. #
    tasarhane on November 14, 2008

    thanks for tip..

    ———-
    selamlar

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