How to: Placing Adsense Strategically Between Posts

Posted By Guest Blogger 9th of March 2006 Blog Design, Miscellaneous Blog Tips

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.
Exit mobile version