Insert Google AdSense Ads between your WordPress Blog Posts

Posted on July 13, 2009, 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 :D

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

22 Replies to "Insert Google AdSense Ads between your WordPress Blog Posts"

  1. Great solution! Though i can only make it appear on 2 posts, the 3rd one doesnt appear. Any idea why? Thank you in advance

    1. @p0w3r, the 3rd one should appear. I have tested the script. What is the URL address where you have implemented the code?

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

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

      2. Hi. This did not work for me at all and I am not sure why. I have a wordpress blog and I found the locations in which to put your commands. I do not have a yellow space indicating an ad is trying to appear or anything. It’s as if I did not add any of the commands.

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

    1. @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;
      
  3. After pasting the code you put, where do i put the code adsense gives me…?what is the custom html code??

    1. @Enrique, I got it solved…I just have one more questions how do i center the banner

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

  4. [...] rest is here: Insert Google AdSense Ads between your WordPress Blog posts Comments0 Leave a Reply Click here to cancel [...]

  5. Hey good stuff…keep up the good work! :)

  6. I was wondering how would I go about having adsense appear every 4th comment ?

  7. how can i add a php code instead of a ad sense code(pure html) ?

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

    1. Sorry, itz my mistake, I was in hurry to check. Now, its working fine.Thanks a lot

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

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

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)