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

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

83 Comments

Johnson July 10, 2008

thanks a lot dear !

i was eagerly waiting for this post…

Rahul Bansal 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. 🙂

Gaurav July 10, 2008

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 July 10, 2008

Thanks for the plug Rahul. Great tip and instructions.

Keith Dsouza 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

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

Gaurav July 11, 2008

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

Rahul Bansal 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. 🙂

aju July 27, 2008

how to get adsense code.

Rahul Bansal July 27, 2008

@aju
Signup for adsense… 🙂

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

Zoekmachine Marketing August 26, 2008

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

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

Rahul Bansal September 18, 2008

@Geek
Your welcome buddy… 🙂

Sebastian 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

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

Deepak Jain November 8, 2008

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

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

Rahul Bansal November 25, 2008

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

Peter Pan November 8, 2008

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

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

Rahul Bansal December 1, 2008

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

replace line:

with line:

Troy Steuwer December 7, 2008

@Rahul Bansal – You rock my world. Thanks!

Troy Steuwer December 7, 2008

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

Troy Steuwer December 7, 2008

oops, nvm, wrong platform 🙁

Troy Steuwer 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?

Troy Steuwer December 7, 2008

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

Rahul Bansal December 8, 2008

@Troy Steuwer
Its ok… 🙂
Btw thanks for confirming above code to be working with WordPress 2.7. 🙂

Raju December 7, 2008

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

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

Rahul Bansal January 3, 2009

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

Tom January 6, 2009

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

t.j detwiler 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:(

Rahul Bansal January 15, 2009

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

t.j detwiler 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

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

t.j detwiler January 18, 2009

plz somebody help me

Sean Brown 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?

Rahul Bansal 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. 🙂

Max the Micro Niche Finder 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! 🙂

Rahul Bansal March 29, 2009

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

reez May 7, 2009

really useful tips..thanks a lot!

Write a Writing 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

Rahul Bansal May 20, 2009

@Amy
You can control ads font from AdSense panel.

RLShawdotcom 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

Rahul Bansal June 2, 2009

@RLShaw
Welcome To Devils Workshop! 🙂

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

KARTHIK KUMAR July 25, 2009

Thanks for the tip i was searching plugin for it.

Dean September 11, 2009

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 October 14, 2009

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

onelargeprawn October 14, 2009

@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 October 27, 2009

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

Robin November 14, 2009

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

f-k December 2, 2009

THANKS for the tips! help a lots!!!

Christina February 21, 2010

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

vivek jain February 22, 2010

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

prashant March 25, 2010

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

website worth June 20, 2010

Thanks for your help! 🙂

Carol July 5, 2010

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.

Stan July 29, 2010

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 July 31, 2010

Hi,

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

sonia September 22, 2010

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 October 7, 2010

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

Dan October 13, 2010

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!

jonas October 17, 2010

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 January 16, 2011

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

Ankit April 11, 2011

Can you tell me how to do this with blogger

Jack April 23, 2011

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/

Bogdan May 12, 2011

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

PunK May 15, 2011

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

ravi September 13, 2011

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 August 9, 2011

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 August 9, 2011

this code

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

Umarfaruk M December 10, 2011

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

Valanz April 27, 2012

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 May 11, 2012

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

Anonymous May 11, 2012

Oh, I just found it.. lol..

<?php if ($adcount

Hemant July 14, 2012

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

tabitha October 4, 2012

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

Luis March 19, 2013

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