Placing Adsense Between Posts - Wordpress

Keywords:
05 Nov 2007

The placement of ads within a blog can have a dramatic effect of the success of your ads. This is particularly true when you talk about Google's Adsense. I'm not going to praise one ad type or the best placement in this post. What I am going to do is show you how to easily place ads (or some other content) between posts.

Personally I have had success placing an ad block between the 1st and the 2nd Post. I will show you how this can easily be achieved with Wordpress.

Wordpress

With Wordpress this can easily be achieved by modifying the template and adding some additional code within the loop.

Just a brief explanation - the Loop processes each of the posts to be displayed on the current page and formats them accordingly.

The basic loop looks like the following, but will be certain to have addition a code in place for various reasons:

<?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>   
  <!-- do stuff -->
  <?php endwhile; ?>
<?php endif; ?>

The following will easily allow you add an ad block between the 1st and 2nd post:

<?php
  $postcounter
= 1; // set counter to 1 for ads
 
if (have_posts()) : ?>

  <?php while (have_posts()) : $postcounter = $postcounter + 1;                             

 
the_post(); ?>
   
  <!-- do stuff -->
  <?php if(2 == $postcounter)  {
  echo
 
'
  <div id="adsbetween">
  <-- Place ad code here -->
  </div>
  '
 
;  }  ?>

  <?php endwhile; ?>
<?php endif; ?>

How it Works?
What we have done is added a variable called $postcounter and assigned it a value of 1. Each pass of the loop increases the value of $postcounter by 1. To vary where the ads appear simply change the value numerical value in the line:

<?php if(2 == $postcounter)

About Us

NZ Web Hosting Internet Technolgies Blog.

Website design, website development, promotion and search engine optimisation.