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.
- Log into the Admin area (Dashboard) of your blog and go to Presentation->Theme Editor.
- Find the link titled ‘Index’ or ‘Main Index Template’ (or something similar) and click on it – the index.php file will now load.
- 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.
- 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.
- 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
- 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.