Close
Close

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
<strong>$counter = 1;</strong>
if (have_posts()) :
   while (have_posts()) :
      <strong>$counter = $counter + 1;</strong>
      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();
      <strong>if(2 == $counter)
      {
         echo 'Adsense code';
      }</strong>
   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.

Problogger.net runs on the Genesis Framework

Genesis Framework

The Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Check out the incredible features and the selection of designs. It's that simple - start using Genesis now!

Comments

  1. Josue says:

    Thanks so much! Man, you put WordPress and people like you guys together and this is like heaven! =)

    The WordPress community is awesome.

  2. Tom says:

    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. racketboy says:

    I’d love to see Blogger ones too.

  4. Wezzo says:

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

  5. Aaron Brazell says:

    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. Elliott Back says:

    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:

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

  8. markus says:

    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:

    Well met, Markus. Fixed that error. :)

  10. Eric Gregory says:

    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. Jon Heizer says:

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

  12. Jhon Hopkins says:

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

  13. Jake says:

    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. Joshua K says:

    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. Some tips for placing adsense inside Blogger posts.

  16. coglethorpe says:

    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.

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

  18. Aaron Brazell says:

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

  19. Aaron Brazell says:

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

  20. Tom says:

    Thanks!

    Ask a question, get an answer.

    That is why we love Darren and all his friends.

    Tom

  21. Eric Giguere says:

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

  22. Martin says:

    very old trick :))

  23. Aaron Brazell says:

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

  24. 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…

  25. Tom R. says:

    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.

  26. Tom R. says:

    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/

  27. marcel says:

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

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

  29. arZan says:

    Any help on this for Movable Type.

    Will be greatly appreciated.

  30. Prince says:

    Hey Aaron,

    Will you tell me

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

    Thanks, bye

    Prince John

  31. Josue says:

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

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

  32. Oyvind says:

    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?

  33. Thilak says:

    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. JonP says:

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

    Thanks, Jon.

  35. Greg says:

    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. Greg says:

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

  37. John says:

    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

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

  39. QiSoftware says:

    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.

  40. Netwalker says:

    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.

  41. jai says:

    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.

  42. Giorgi says:

    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

  43. thedude says:

    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.

  44. thedude says:

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

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

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

  47. Asha says:

    I’ve just written up a tutorial on how to place Adsense between posts in TypePad.

  48. If you can’t use an imagemap, or if you’d rather access the editorials through a more conventional table of contents, all 28 are also arranged by newspaper and date. Clicking on the paper at left will take you to that list. stylishcn com Clicking on the paper at left will take you to that list importers-exporters http://www.importers-exporters.com/jewelry_watches.htm

  49. Best Numbers says:

    I want to say big thanks to:
    http://weblog.ashaland.com/ashaland/2006/06/how_to_place_go.html

    for great ‘step by step’ tutorial that helps me a lot.

  50. Oscar says:

    Can anyone here help me figure out a way to only have ONE or TWO ads appearing in my main column after the first two pots?

    My problem is that all THREE of my ads are appearing in the main column after posts, and an addition MIDDLE column I maid specifically for ads is EMPTY because the main column is using up all the ads I am allowed.

    Can anyone help me find a way to limit how many ads are after my posts to lets say one or two?

    Thank you very, very much. I really do appreciate it!

Trackbacks

  1. [...] 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.   [...]

  2. [...] Placing Adsense Strategically Between Posts explica precsamente eso: cmo colocar la publicidad de AdSense (o cualquier publicidad, o cualquier otro contenido) entre las entradas de WordPress, modificando su bucle. [...]

  3. [...] How to: Placing Adsense Strategically Between Posts  [...]

  4. [...] How to: Placing Adsense Strategically Between Posts Adsense Tips: How to place your ad between your blog posts in WordPress [...]

  5. [...] How to: Placing Adsense Strategically Between Posts (tags: wordpress adsense) [...]

  6. [...] Placing Adsense Strategically Between Posts read the comments (tags: AdSense Blog_Tools) [...]

  7. [...] In early 2006, Darren of Problogger initially released a tutorial for this but at the last comment someone said that it wasn’t supported on version 2.x and only version 1.5. [...]

  8. [...] can also check out this site and this site for other ways of adind code betweeb blog [...]