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

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;

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;
      if(2 == $counter)
         echo 'Adsense code';

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.
  1. […] How to: Placing Adsense Strategically Between Posts  […]

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

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

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

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

  6. I want to say big thanks to:

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

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

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

  9. i tried this on wp 2.0X and it didnt work.

    another blog of mine which is still 1.5 (haha, yes i know) worked well.

    would appreciate it if there is a revised tutorial for 2.0x and above.


  10. […] 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. […]

  11. I stumbled on a different method that works on version 2.x

    Just posted the tutorial over at my place with reference to the original source. I explained the process in layman so it’s easier to understand. :)



  12. Brandon Hopkins says: 02/23/2007 at 8:58 am

    I’m trying to get it to work, and it’s kinda showing weird things instead of adsense…see after the first post:


  13. hi..
    while this tweak worked on a normal theme i had before.
    the current theme im working on had tags closed at every line

    Which will lead to printing out of these code when one pastes these.

    Then i figured that the addition will make them work..
    so if the code is being printed out for all of you instead the ads being displayed in the desired postion

    try the tags…
    ie, paste these in the positions Darren has mentioned

    Hope this wil help someone if they experience a problem similar to mine. and thus saving precious time for producing contents…

    and also thanks to Aaron , and also Great blog Darren :D !!

  14. Ohh the code got stripped

    here it is .. :)

    remove spaces between ? and > or

  15. Thank you!!

    I’ll try this on my blog… :-)

  16. I know this is a old post, but I keep finding it for help with this problem. But it did not work for me because of WP 2.0, here is the link that worked for me


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

A Practical Podcast… to Help You Build a Better Blog

The ProBlogger Podcast

A Practical Podcast…