How to Insert Adsense Ads Between Posts in WordPress Blogs

How can you insert Adsense ads between posts on WordPress archives, categories, tags,  index, search and homepages. As you learnt about how to insert Adsense inside articles, inserting between WordPress post summary pages is a little different.

Insert Ads Between Posts in Archives

While editing your WordPress theme appearance, open index.php or archive.php. These files control the theme template for the homepage, index pages and archive pages (category and tags). The secret is to insert the Adsense code as seen on our site is below.

<?php $postnum++; if($postnum%5 == 0) { ?>
<style>
.responsive { width: 300px; height: 250px; }
@media(min-width: 500px) { .responsive { width: 336px; height: 280px; } }
</style>
<ins class="adsbygoogle yourclass"
style="display:inline-block"
data-ad-client="ca-pub-12345678"
data-ad-slot="12345678"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php } ?>

This code uses Adsense responsive unit to show small and large rectangle ads depending on the screen size. I had told you this large rectangle ad setting is the highest paying combination to target both 300x250px and 336x280px ads.

Add Adsense loader script once

It is best to load Adsense script once. You can place it between the <HEAD> tags of your html code, such that it loads only once for multiple ad units. Since it uses asynchronous adsense code, it does not slow page loading time and can be used at the top the code.

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

Posts Per Page

The important thing to note here is you need to check how many post summaries you display on each category, tag or index pages. This can be done via Settings > Reading

archive posts number

So you can choose how many post summaries to display on any such archive page. This example displays 8 post summaries on each page.

Display Multiple Adsense Ads

With code setting at $postnum%5, it will insert an Adsense ad after every 5 posts. So if you have 8 posts displayed, it will display the ad only once on this page. Now it is your choice how many ads you want to display on a page. Note you cannot display more than 3 ad units per page.

If you increase the posts per page to lets say 12, then this will display 2 ads, one after the 5 th post and one after the 10th post. Alternatively you can change the count code to  $postnum%3, which will cause the ads to load after 3 ads and so on.

Here is how it will look like.

adsense middle ads

Share with friends

About the Author: P Chandra is editor of QOT, one of India's earliest tech bloggers since 2004. A tech enthusiast with expertise in coding, WordPress, web tools, SEO and DIY hacks.