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

    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.

    Cheers!

  2. Danny Foo says:

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

    http://www.dannyfoo.com/blog/2007/01/how-to-insert-horizontal-banner-between-1st-and-2nd-post/

    Cheers.

  3. Brandon Hopkins says:

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

    http://www.freebusinesscards.info

  4. NGeN says:

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

  5. NGeN says:

    Ohh the code got stripped

    here it is .. :)

    remove spaces between ? and > or

  6. Thank you very much!.I have a method to do it on Blogger blog.
    http://www.thesitewriter.com/2007/07/how-to-place-adsense-ad-unit-between.html

  7. Alex says:

    Thank you!!

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

  8. Mike says:

    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

    http://www.lancelhoff.com/2007/02/28/adding-adsense-ads-between-posts/

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