Facebook Pixel
Join our Facebook Community

How to: Placing Adsense Strategically Between Posts

Greetings, fellow Probloggers (and those who aspire to be probloggers). It’s somewhat surreal to be posting here. Darren’s highlighted who I am pretty well (though I’m not sure where he dug up that photo!), so I’ll simply add that my strength lies in WordPress. I have been an active part of the WordPress development community for some time, so it will be natural to pass along some things I know from my experience with the platform. Some of the tips I write about will be very easy for some, and hopefully there will be entries that challenge even the most advanced. As long as someone is learning, I’m happy.

A few days ago, Darren posted an entry about ad placement that took an interesting turn in the commentary that followed.

Commenter Tom asked:

What is the plug in that you use to put the ads between the 2nd and 3rd post, or do you do so manually?

That, my friends, is the tip of the day. WordPress is a very flexible platform that allows for quite a bit of “munging” to make things work right. For starters, the basic building block of WordPress is a block of code called “The Loop”. It is called that because, literally, it is where the posts for a given page are “looped” through repeatedly to be displayed on the blog.

The standard Loop in the index.php file, is a block of code that might look a bit like this:


if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_content();
   endwhile;
endif;

A basic explanation of this block of code is:

  • If there are posts to display on a page, then
  • We begin processing the posts one by one as long as there are posts to display

This is, in fact, the absolute minimum that a Loop requires and it will get the job done. The blog won’t be much to look at, but it will display entries successfully. But how do we know where to put the Adsense code?

Here’s the trick: We can actually count how many times Loop has cycled and use that information to our benefit. That same block of code with a counter might look something like this:

// Set Counter to 1, First Post
$counter = 1;
if (have_posts()) :
   while (have_posts()) :
      $counter = $counter + 1;
      the_post();
      the_content();
   endwhile;
endif;

Here we’ve dropped a variable, $counter, into the code, set its initial value to 1 and simply incremented it by 1 after every pass through the Loop.

Now we can worry about determining where to put the Adsense code.

// Set Counter to 1, First Post
$counter = 1;
if (have_posts()) :
   while (have_posts()) :
      $counter = $counter + 1;
      the_post();
      the_content();
      if(2 == $counter)
      {
         echo 'Adsense code';
      }
   endwhile;
endif;

Bam Bam Bigelow. That’s all there is to it. Drop your own Adsense code in there and you’re off to the races. Just as a precautionary note, make sure you place the if conditional and Adsense code before the end of the Loop. Most forms of the Loop end before endwhile, but there is a chance that your Loop doesn’t use this same format.

I hope this helps. Feel free to drop requests here in the comments. I’d love to tackle the issues that you as bloggers face and see if we can’t discover more ways to make our blogs sing.

About Guest Blogger
This post was written by a guest contributor. Please see their details in the post above.
Comments
  1. Thanks so much! Man, you put WordPress and people like you guys together and this is like heaven! =)

    The WordPress community is awesome.

  2. Great tip! You wouldn’t happen to have a similar tip for the Blogger tool, would you? Most of the Blogger scripts are simple javascript. Would changing the index page type to PHP allow this to run (assuming the hosting service supports PHP)? Wouldn’t you then have to change all of your pages to php as well, like the auto-generated archive pages? Thanks in advance.
    Btw – enjoyed your podcast interview a few weeks ago (think it was with Yaro…)
    Tom

  3. I’d love to see Blogger ones too.

  4. Yeah great stuff, as racketboy says love to see Blogger ones

  5. Aaron Brazell says: 03/10/2006 at 12:53 am

    The Loop is only WordPress though other systems have similar concepts. The thing to realize, The Loop (or similar thing) is executes on the server prior to any information being sent to the browser. In the case of Blogger, unless you have access to Google’s servers, you’re not likely to have the same results.

    If people are interested in seriously leveraging their blog, I usually recommend a move to a self hosted solution such as WordPress. Or even Moveable Type if you can figure out how to get it working. ;)

  6. I usually write something like

    if(2 === ++$counter)

    to get read of the increment line. You can also put your ads every 2nd, 3rd, 4th, etc post with a finite field:

    if(++$counter % 2 == 0)

  7. Aaron Brazell says: 03/10/2006 at 1:09 am

    Elliott, me too. Just starting out with code that might be a tad easier to understand. :)

  8. You start with counter=1 and then you use counter=0. It’s a small mistake.

    I also use some kind of code like this. Works fine.

  9. Aaron Brazell says: 03/10/2006 at 1:29 am

    Well met, Markus. Fixed that error. :)

  10. I’ve been waiting for someone to write about this. Awesome!

    Now if only someone would show me how to do this in Drupal…

  11. Nice post and welcome to ProBlogger! Being a programmer, I will look forward to your posts.

  12. Thanks for the tip, it really works for me and my blog

  13. Hey great post Aaron!
    You made this simple. So many others hae tried to help people with this yet you put it in easy steps.

    I am sure thousands are very happy right now and you made their day.

    If you get a chance can you head to:
    Raise Your CTR by 200% and tell me what you think.

    Thanks.

  14. Oh yeah, that’s some goodness. Thanks for this tip! (Elliott, good coding tip there too.. I’ll be using that a lot in the future).

  15. This is the stuff! I’m about to update my theme to include this. The new layout may well work better than the left hand skyscraper I use now.

  16. I used to use an old plugin called Moose Candy to insert content between posts. It would let you insert whatever you wanted in between specific posts. So you could have an AdSense ad between the second and third posts, a quote of the day at the top, or just about anything you can imagine. Unfortunately, the person who wrote the plugin has gone away, and it’s no longer available as far as I know.

  17. Aaron Brazell says: 03/10/2006 at 7:30 am

    I’m glad this topic is being so well recieved. Makes me excited for the future. Something I’m thinking about doing is a mini-series on creating a plugin but I’d like to do one that the community can benefit from when the series is all done and the plugin is available. So folks, feel free to pitch a plugin to me and let’s see where the dust settles? :)

  18. Aaron Brazell says: 03/10/2006 at 7:36 am

    Michael— you’re on the WP Testers mailing list, no?

  19. Thanks!

    Ask a question, get an answer.

    That is why we love Darren and all his friends.

    Tom

  20. I don’t use WordPress (I use blojsom) but I did a similar thing with my blog, and just wanted to add something: don’t forget to use the alternate URL feature to display other ads if AdSense can’t find an ad to display. You can easily create your own Google-like ads to promote products sold via affiliate links (like from ClickBanks or Amazon) so that you never have any empty space where the ads would be…. see the cheezy “Ads by Eric” on my blog, for example (scroll past the first three, those are real AdSense ads).

  21. very old trick :))

  22. Aaron Brazell says: 03/10/2006 at 10:25 am

    Indeed! But apparently one that many readers did not know. :)

  23. Thank you, I have been wondering how to do this for ages. Even though some say it is a “very old trick” I had not found how to do it.

    Probably using the wrong search terms…

  24. I managed doing the same though using a different method. Yes, it’s possible to do something similar using other way. But mine is, I would say, easier to manage. I got the wonderful http://www.acmetech.com/blog/adsense-deluxe/ plugin which allows me to plug the ad into the script easily.

    My code is only added at 3 different places in the templates files:
    1. In the theloop.php, find

    –>
    then add

    This code will output a ad every 3 posts (but beware, google TOS says no more than 3 ads per page, so I’ve warned you).

    2. In the sidebar.php
    just before the div class clear, add

    This code output the ad in the sidebar

    3. In single post
    add

    after the call.

    This will output the ad just before the comments and the comment box.

    Using ad sense deluxe (which you should have actived), create a block called MidPage (which would generally contain the code for a 468×60 banner) and a Sidebar (which contains any scrapper kind) and you’re done. Might be a bit long, but it’s easier to manage after.

  25. Since it seems I can’t get my code to show properly, I might suggest you go check the “same” text on my blog post (done for the purpose)

    http://excitris.com/2006/03/09/add-adsense-in-your-wordpress-blog/

  26. you can do the same with Drupal… after you hack the code

  27. Yeah, the Moosecandy plugin does a great job letting you insert AdSense (or anything) after any specific post. You install the plugin, and then all you have to do is edit a text file, specifying:

    The number of the post
    The code you want to insert

    You can put in anything, from ads to plain old text, and it involves very little actual thinking. A simple Google search will find the plugin, although some download sites no longer work.

  28. Any help on this for Movable Type.

    Will be greatly appreciated.

  29. Hey Aaron,

    Will you tell me

    How to wrap the adsense ad with Post Text in blogger blogs?

    Thanks, bye

    Prince John

  30. There is also this page that explain it very well with different options:

    http://www.tamba2.org.uk/wordpress/adsense/

  31. What if you want to use some php code instead of pasting the adsense code directly?

    If you use the adsense deluxe plugin (http://www.acmetech.com/blog/2005/07/26/adsense-deluxe-wordpress-plugin/), you place the ads with code.

    From the help page:

    Name
    This is the name by which you reference an AdSense block of code when creating posts. For example, if you name a block “wide_banner”, you would insert into your post
    “”.

    But if I substitude

    echo ‘Adsense code’;

    with

    echo ‘’;

    I get an error?

  32. […] To change the frequency of the Adsense ads, just change the number ‘3′ above to a different number.   If you want to display one ad only after the 2nd , 3rd , 4th etc and not have it repeated, you can refer to the original tips at ProBlogger on how to do this.   […]

  33. i wanted to put adsense link unit below every post so i put my adsense code in the loop. but it displays ad only on first 3 posts . plz help me darren

  34. And remember to turn off Adblock from Firefox! Confuses things for simple people.

    Thanks, Jon.

  35. Thanks! One quick question.

    I’m using a 3-column theme ( WordPress blog developed using Semiologic CMS, original theme design by Mesoconcepts, modified). The call for the posts (if statement) isn’t located in the index.php file. What file, other than the index.php file, might it be in?

    I understand how to edit the loop, just can’t find it.

    Learning something new about WP everyday. Any assistance you may provide is appreciated.

  36. Oops! Mis-typed my website address in the previous post. This post has my correct address (www.eoecho.com/allthingsmercedes/).

  37. […] Placing Adsense Strategically Between Posts explica prec�samente eso: c�mo colocar la publicidad de AdSense (o cualquier publicidad, o cualquier otro contenido) entre las entradas de WordPress, modificando su bucle. […]

  38. That’s a great tip and I thought the pro wrestling reference was way cool! (Bonus points if anyone else can figure it out – the points don’t matter, but it’s nice to know they’re there)

    John

  39. Blogger: Followed the instructions from comment 15 and worked like a treat. Thanks

  40. I wrote a similar function in JavaScript — Ads Under First Post. Awhile back, Google claimed my code was not what they provided and because of this could not anaylze why I had problems with impression counts. Your echo above — would be equavilent to the changes I made.

    I must admit, Google — in later communiqués, did not reiterate their issues with the JavaScript function.

    A warning for those considering some sort of code trick to insert AdSense ads in posts.

  41. I implemented this in my blog.

    I have one link unit at the top of the page, one ad unit on the sidebar of my blog and the adsense for search.

    But after doing the changes explained here, it only appears the one that goes between the post 2 and 3, but the one in the sidebar it’s not showing any more.

    Does this means that I have more than the allowed ones? as I understand I can put 1 link unit, 1 adsense for search and 3 ad units in the same page. So I’m not going over the allowed quantity. Or it means it doesn’t has ads that match my blog?

    Thanks in advance.

  42. I implemented this in my blog.

    I have one link unit at the top of the page, one ad unit on the sidebar of my blog and the adsense for search.

    Download Toolbar RSS (Free) & with advanced features. You can send messages to all users.

  43. Hello, Gr8 ppl!

    I have question, in my index.php there is:

    “>

    so, can anybody modify it so I can diplay ad after every 5th post?

    Thank you in advance

  44. thedude says: 05/06/2006 at 3:03 am

    Hello Everyone,

    I am using a wordpress theme, but my code looks like this…

    How would I modify it to show my google ads between posts? Additionally, if I use this method, will my google ads between posts be shown in my permalinks and/or categories?

    I am very new to the blogging world and am just in the process of setting up my blog the way I want it. Please help me if you can. Any advice would be very appreciated.

  45. thedude says: 05/06/2006 at 3:04 am

    It did not show my code on here! I don’t know what is happening!

    Here is my code again I have removed the “” from it.

    ?php if (have_posts()) : ?
    ?php while (have_posts()) : the_post(); ?
    ?php require(‘post.php’); ?
    ?php endwhile; ?

  46. thedude… WordPress strips code so you have to fudge it.

    In answer to your question, it doesn’t really look like you did what I suggested above. If you go back and read the entry and follow along, you’ll get your ads between posts. Luck to you!

  47. If you want adds with all your posts on the blogger blogspot blog you can put the code inside the <Blogger> tags and set the maximum number of posts to 3. You would have to make sure they don’t show up on the archives by using <MainPage> tags around them.

    But if you want more as 3 (or more freedom over placement) you put the ad code inside the <BlogItemCommentsEnabled> tags then disable the comments for all but 3 posts.

    Do adjust the blog settings to “new posts have NO comments” or you will end up with more as 3 adverts per page(more as allowed)

    http://categories.blogspot.com/2006/04/adsense-between-posts.html
    Adsense between posts

A Practical Podcast… to Help You Build a Better Blog

The ProBlogger Podcast

A Practical Podcast…

Close
Open