Top

Adsense Tips: How to place your ad between your blog posts in WordPress

March 11, 2006 by  

OK, time for another Adsense Tips.

I had been looking for a way to do this or a plugin that can help me achieve the same effect. I’d probably end up doing it myself if I couldn’t find anybody who has posted about this.

Anyway, Aaron Brazell, the new ProBlogger columnist posted this tip recently.

If you look at Sabahan.com main page, you’ll notice that I’ve placed the Adsense rectangle ad after the 3rd, 6th, and 9th posts. BTW, I might change this setup in the future.

The way I do this is by editing the following block of code in my index.php file.

 

Yours may be a little different from mine but it should resemble either this:

 

<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>

Some code here…

<?php endforeach; else: ?>

 

Or this

 

<?php while (have_posts()) : the_post(); ?>

Some code here…

<?php endwhile; ?>

 

This is where wordpress loop through and display your post one by one as long as there are posts to display.   What I’ve done is to add a counter to count if a particular post is the 3rd, 6th, 9th, 12th and so on. If that condition is true, then my Adsense code will be placed at the end of that post. So, for the first code snippet example above, add   <?php $counter = 0; ?>   before

 

<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>

 

Then add the following code to display your Adsense code after the 3rd , 6th , 9th etc. posts only.  

<?php if(++$counter % 3 == 0) { Echo “Your Adsense code here”; } ?>  

 

It must be added before the end of this code: 

 

<?php endforeach; else: ?>

 

Your final code should look like this. For the first code snippet    

 

<?php $counter = 0; ?>

<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>

Some code here…

<?php if(++$counter % 3 == 0) { Echo “Your Adsense code here”; } ?>

<?php endforeach; else: ?>

 

And if your index.php code resembles the second code snippet, your final code should look like this:

 

<?php $counter = 0; ?>

<?php while (have_posts()) : the_post(); ?>

Some code here…

<?php if(++$counter % 3 == 0) { Echo “Your Adsense code here”; } ?>

<?php endwhile; ?>

 

To change the ad intervals between your posts, 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 this is done.  

My advice is not to go overboard, plastering your blog with ads all over thinking to make more money at the expense of your readers’ experience. In such case, what’s more likely to happen is you’ll annoy them, and risk losing your regular or potential readers.

You can check out my earlier post for more Adsense tips for blogger.

Did you enjoy this post? Please subscribe via RSS or email.

Related posts

Comments

RSS feed | Trackback URI

4 Comments »

Comment by LcF
2006-03-11 01:49:58

Erm.. we have the code 1 year ago in WordPress support forum. I also published a adsense random placement code.

 
Comment by Gaman
2006-03-11 02:32:55

I guess I was searching using different keywords. Anyway, cheers mate.

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post

Bottom