How to Show Ads between Posts on your Blog

This article will show you how to place AdSense code (or any other ads) between posts on your WordPress blog’s main page – the same approach can be used to place ad blocks in your archive pages (including category and author archives).

I’m going to explain the conceptual part first (yes, I just like to hear myself talk) and give code examples later – if you want to skip to the code, just scroll down.

How-To – Basic Concepts

The basic concept (so you can port it to a blogging platform other than WordPress) is that you want to place ads after X number of posts on the main page.

Let’s say that you want to put ad blocks after the 1st post and the 3rd post – so you would then find a way to dynamically count the number of posts that are being shown and set it so that after the 1st post and the 3rd post.

In WordPress the main process used to show posts to visitors is called ‘The Loop’ (read this and this for more info). In programming terms it’s a while () loop that continues to show posts one by one until it meets a condition that tells it to stop (such as a WP setting that limits the blog to show the latest 15 posts on the main page) or if there are no more posts.

If we’re able to count how many posts the while () loop has already shown (by declaring a counter variable outside the loop and incrementing it inside the loop, and then using the value of that counter as a test condition), we can easily program our blog to show ads after a set number of posts.

That’s not where this stops though – you can also edit your theme outside the loop to put ads at the start and/or end of each index post (right before the previous / next post navigation), without any fancy counter stuff.

Ok, now for the code and the walkthrough.

How-To – Step-by-Step

Remember to bookmark this page for future reference if you don’t have time to implement this right now.

The following walkthrough will show you how to put ads after the 1st and the 3rd posts on your blog’s main page.

You can copy-paste the code below – but please remember that you might need to customise it to suit your theme / adsense plugin.

  1. Log into the Admin area (Dashboard) of your blog and go to Presentation->Theme Editor.
  2. Find the link titled ‘Index’ or ‘Main Index Template’ (or something similar) and click on it – the index.php file will now load.
  3. Somewhere in your index.php file (if you use Firefox, press ctrl-F and paste ‘have_posts()‘ in it), there will be this line: .

    Essentially, this is the starting point of the WP ‘Loop’ which is used to display blog posts on your main page and often the archive, author and category pages as well.

  4. Once you locate this line, add the following line before it:

    and the following line after it:

    This will help you dynamically count the number of posts that have already been shown by the Loop, so that when the first and 3rd posts are shown, we can trigger a command that allows us to show the adsense code.

  5. If you scroll down, you’ll see the code for displaying individual posts and after that, there will be a block of code similar to this:

    At the very least, you’ll see a statement.

  6. Right before it, copy-paste the following block of code:

    ' ?>

    ' ?>

    Please, please, PLEASE – repace the bit inside the php echo command (in this case, ) with your own Ad code. I’m using the Semiologic Ad Spaces plugin, which allows me to place a simple tag and indicator of which adblock to show in the template code. From what I understand, the AdSense Deluxe plugin does the same thing. If you copy-paste the AdSense code directly between the single quotes ” like the following example, that will work too:


    ' ?>

    I would still recommend using a Plugin here – it makes things MUCH simpler.

    You can also vary the numbers in the if statements to set things up so that the ads ONLY show after the 2nd post, or something similar.

    So instead of writing:

    You would write:

And here’s a bonus tip – on index pages, monetize the end of your index pages as well. To make that work, in your index.php file, scroll to the end and find the code that shows the previous / next posts navigation.

Right below it, add your AdSense code.

And remember, you can also display any other type of ads here – maybe you want to show Chitika ads, or affiliate ads, or even house ads promoting your forum. The monetization possibilities are endless.

Hope you enjoyed this post and for everyone who posted in the AdSense Placement Thread, I hope this helps you follow part of my advice – looking forward to seeing you guys optimize your ads.

Also read these articles by Chris Garrett:

  • How to create Intelligent Blog Ads

    If you want to squeeze every last drop of revenue potential out of your blog without annoying your loyal visitors then you need to be a bit clever about the way you display advertising.

  • How to create Intelligent Blog Ads – Part 2

    What can we do about recognising first-time readers versus regular readers? The idea is a first timer is more likely to click ads and less likely to sign up, while a regular reader is more likely to subscribe to your feed and also more likely to get annoyed by over the top advertising.

And if you have more questions on AdSense, let me know here.

Comment with Your Facebook Account

Comments

  1. Ryan Caldwell says:

    What a great post. I wish I had seen a post like this when I first started playing around with WordPress and post-level ads.

  2. LaLindsey says:

    I have used this plugin (CG-InBetween) by David Chait for about a year now. It does pretty much the same thing – allows you to place any type of content between whatever posts you determine. It only works on the index page though, I believe. You can choose to put things between any number of posts (say after the 6th post you want to display a banner ad, or adsense). You can put in php code, javascript, html – anything.

  3. hitzatak says:

    Thank you Ahmed. You really taught me a few great tricks! I’ve implemented it on my blog. Keep these AMAZING posts coming!

  4. Anirudh says:

    Thats good advice, but I guess we have plugins like adsense manager and adsense deluxe that helps in placing google ads between the posts.

    I think its much better and easy…what do you say.

  5. Very Informative. Thanx alot.

  6. Thank you so much! Your step-by-step was so easy to follow and it worked perfectly. I really appreciate your guide!
    Homemaker Barbi

  7. Great post Ahmed…

    Your post really resolved my problem…
    I was confused of placing ads between posts. When I found your post, I tried it immediately and now the ads has been showing between posts.

    Thanks again

  8. ??? ? ?????? ? ????: ?????? ??????? ? ???? ??????, ??????? ???, ????????? ???, ?????????? Flash-????, ?????, ????????? ???????, ????????????? ???????????? ??????????? ??? ??????, ?????? ????????, ??????? ????????, ??????

  9. tsenks for this post

  10. If webmaster now PHP? for him it will be very easy to write script like this

  11. It helps me a lot, now I can show ads after 2nd and 6th blog post

  12. I have put ads in differnt location in my blog but they are only shown on two places but rest are not displayed similarly i have checked many a times to display ads between post thay are also not been displayed. what to do????

  13. With so much enthu i started reading your post and thought it will solve my problem. But your post itself has a problem. It doesn’t display the code that i should use. Please correct it. It will be of great help.

    • Raj Dash says:

      Hmm. That must have happened when the site moved over to WordPress. I will have to talk to the team.

  14. I was curious how to do that — great tutorial!

  15. I can’t seem to see the code you say to insert. I’m using the new Firefox 4 if that makes any difference.

  16. thanks bro
    lovely tutorial

  17. I AM SO HAPPY TO GET IT, NOW I CAN EARN MORE THEN BEFORE

Trackbacks

  1. [...] How to Show Ads Between Posts on your Blog – Ahmed BilalTakeaway: [...]

Speak Your Mind

*

* Copy This Password *

* Type Or Paste Password Here *