Insert Adsense Ads within WordPress Loops without a Plugin

You must have come across several WordPress plugins for integrating Adsense into your WordPress powered blogs be it in the sidebar, between posts, in between the content and other areas like footers and headers. Using a plugin is really simple, isn’t it? Install an Adsense plugin with a click of a button and then link your Adsense account to it, specify the dimensions of the banners and there you go!

The ads are running on your blog.

But then one must also understand the additional load that it adds to your server when you increase the plugin count on your WordPress blog. So in this article, we will look at ways using which you can manually place the Adsense banner [or any other AD serving agency] by implementing some really simple code snippets. We will provide you the code, don’t worry!

I am sure most of you follow the method of placing the advertising banner code manually in the WordPress files be it in single.php, index.php, archive.php or the 404.php. Even beginners can do it manually without much effort. All it requires is applying some logic on where exactly to place the code. Placing the Adsense code below the post title / before the comments section is easy, placing below the navigation bar is easy, placing it in the sidebar or even in the footer is also easy.

But it is a bit tricky when it comes to placing the Adsense code between 2 posts or in between the article body. That is exactly what we are going to focus today. In our first segment, we will look at the possible ways to place the Adsense code in between the posts be it in the homepage or in the archives page.

Insert Adsense Ads within WordPress Loops

At the end of this segment, you will be able to place the Adsense code such that you will obtain something as shown in the screenshot below:

The arrow marks suggests the location where your banner will be made visible after using the following code snippets in your WordPress theme files. Follow the steps given below:

1) Open the files where you would want the above format to be displayed i.e., a post along with it’s excerpt followed by AD banner and another post with excerpt will follow below that.

2) Usually you will have to change 2 of your theme files: index.php [homepage of your blog] and archive.php [category page] where this format can be implemented.

3) These files are located in the following directory: “your-blog-root-directory/wp-content/themes/your-current-theme-folder/

4) Find for the following code in the above mentioned files. It can be either of these 2 formats:

<php if (have _posts ()) : ?> <php while (have_posts()) : the_post(); ?>


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

Replace this code with…

<?php $postcounter = 1; 
if (have_posts()) : ?> 
<?php while (have_posts()) : $postcounter = $postcounter + 1; 
the_post(); ?> 
<?php if(4 == $postcounter) 
{ echo ' <div id="adsbetween"> 
<center> YOUR_ADSENSE_CODE </center> 
</div> ' ; 
} ?>

5) Replace YOUR_ADSENSE_CODE with your banner code. The “if(4 == $postcounter)” should be changed according to your convenience depending on the position where you want the Advertisement to be displayed.

6) That’s it. You are done!