Insert Google AdSense Ads between your WordPress Blog Posts
Posted on July 13, 2009, Filled under PHP, Tutorials, Wordpress,
Bookmark it
According to Google AdSense Program Policies you are allowed to add up to three ad content units in a page. If you have over 10 posts in a page and you wish to insert Google ads between them, you may need to specify the position of each advertisement (for instance, the first is after the 3rd post, the second after the 7th post and the last one is after the 10th post) and make sure that the limit of 3 ad content units is not exceeded. In this short tutorial I will teach you how you can do that. The code can be applied in every page where posts are shown including the index, the archive, the tags and the search results.
I will work with the index.php file from WordPress Default Theme. The code from other themes is pretty much the same so you just need to pay attention to the details ![]()
First, let’s setup the ads. We will use an array that will store the advertisements’ codes. Find the code:
<?php while (have_posts()) : the_post(); ?>
and replace it with:
<?php
/*
Key = Position of the Ad
Value = The AdSense Banner Code
*/
$advertisements = array('3' => '<!-- adsense ad code 1 here -->',
'6' => '<!-- adsense ad code 2 here -->',
'9' => '<!-- adsense ad code 3 here -->');
$i = 1;
while (have_posts()) : the_post();
?>As you can see each banner code has a key assigned. For example, the second element has the key 6. We will use this value to show the banner after the 6th post on the page (in the case there are at least six results).
Now, find the line:
<?php endwhile; ?>
and change it with:
<?php
if(array_key_exists($i, $advertisements)) {
// you can add custom HTML code here
echo $advertisements[$i];
}
$i++;
endwhile;
?>How it works?
As you can see I have declared the variable $i that auto increments inside the while statement. The $i number is incremented at the ending of each loop. Here we use array_key_exists() to see if there is any key equal with $i. For instance at the 9th loop the value of $i would be nine and will be verified if there is any key equal with 9 in the $advertisements array. If it is, the advertisement is shown.
Note: You can keep as many elements you want in the array. You can add for instance additional banners beside the ones from Google AdSense.
That’s all! If you have any questions please post them! Happy coding ![]()
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- July 13, 2009
- article by Gabriel C.
- 21 comments
Related Posts
Easily Integrate your ‘Buy Sell Ads’ Zones in your WordPress Blogat August 2, 2009 with 1 comment
How to Separate WordPress Trackbacks and Commentsat July 13, 2009 with 2 comments
PHP: Detecting Google Chrome Browserat October 6, 2008 with 7 comments
Integrate Your Buzz Stream Into Your Webpage: Google Buzz Widgetat February 16, 2010 with 1 comment
Embed Google Maps into your website: gMap jQuery Pluginat March 5, 2010


21 Replies to "Insert Google AdSense Ads between your WordPress Blog Posts"
July 14, 2009 at 2:05 AM
[...] See original here: Insert Google AdSense Ads between your WordPress Blog posts [...]
July 19, 2009 at 10:36 PM
Great solution! Though i can only make it appear on 2 posts, the 3rd one doesnt appear. Any idea why? Thank you in advance
July 19, 2009 at 11:10 PM
@p0w3r, the 3rd one should appear. I have tested the script. What is the URL address where you have implemented the code?
July 22, 2009 at 1:08 PM
@Gabriel,
Hi Gabriel,
thanks for the reply
actually i can make it appear now but i have another issue
i get a blank area after the 4th post, so i guess it’s still lopping and i dont know how to solve it. not the greatest at coding
thanks again!
here’s the url: http://tinyurl.com/ltjtxn
July 23, 2009 at 9:56 PM
@p0w3r, are you sure you followed the exact steps? It should really work. How many results per page you should see? Perhaps you have updated the blog settings to show only 4 results in each page.
July 23, 2009 at 7:24 PM
Hey it is a great tips. I have trial your advise and success. But Can you help me what should i do if i want to make ads show at the beginning of the post not the end of the post.
Thanks
July 23, 2009 at 10:03 PM
@ali, move the following code:
if(array_key_exists($i, $advertisements)) { // you can add custom HTML code here echo $advertisements[$i]; }between while (have_posts()) : and the_post();
The final code should be:
while (have_posts()) : if(array_key_exists($i, $advertisements)) { // you can add custom HTML code here echo $advertisements[$i]; } $i++; the_post(); endwhile;July 29, 2009 at 3:04 AM
After pasting the code you put, where do i put the code adsense gives me…?what is the custom html code??
July 29, 2009 at 6:20 AM
@Enrique, I got it solved…I just have one more questions how do i center the banner
July 29, 2009 at 5:49 PM
@Enrique, add the banner code inside a stylish DIV like this:
<div style="width: 470px; margin: 0 auto;"><!-- AdSense Code Here --></div>If you are using a leaderboard then set a 730px width.
I do not recommend you to use the <center> </center> tag as it is deprecated and should be avoided. It is not supported in XHTML 1.0 Strict DTD.
Note: The DIV’s style can be added in an external CSS file.
September 1, 2009 at 7:40 PM
This site rocks!
September 2, 2009 at 6:48 AM
[...] rest is here: Insert Google AdSense Ads between your WordPress Blog posts Comments0 Leave a Reply Click here to cancel [...]
September 7, 2009 at 2:10 PM
Hey good stuff…keep up the good work!
September 23, 2009 at 3:05 PM
Nice site!
November 23, 2009 at 5:03 AM
I was wondering how would I go about having adsense appear every 4th comment ?
November 25, 2009 at 9:15 AM
[...] http://www.bitrepository.com/insert-google-adsense-ads-between-your-wordpress-blog-posts.html [...]
December 10, 2009 at 2:02 AM
how can i add a php code instead of a ad sense code(pure html) ?
January 19, 2010 at 12:11 AM
Thanks for this very useful code.According to my index page, I’ve 8 posts & I would like to show two adunits & 2 link units between each two posts.Rightnow, I’m using your code it shows 3 ads without any problem but I need to show 4 ads. Could you please help me on this?
January 19, 2010 at 12:15 AM
Sorry, itz my mistake, I was in hurry to check. Now, its working fine.Thanks a lot
March 23, 2010 at 10:04 PM
you rock dude.. I’ve entered this code to my blog and is working very very well. Thanks
. Btw, can you tell me how to make a footer like yours?. Thanks again
July 4, 2010 at 6:49 PM
Much thanks for yet another first-rate post. I am always trying to find good WordPress tricks to recommend to my own readers. Thank you for taking the time to write this tutorial . It’s just what I was looking for. Truly awesome post.