Insert Adsense Between Posts & Article In WordPress without Plugin

In a Nutshell

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 a plugin within a click of a button and then enter your published ID in the field given, 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 you follow the method of placing the advertising banner codes 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.

Also read similar articles:

WordPress: How To Show Related Posts With Thumbnails Without Plugin

How To Show Most Commented Posts With Thumbnail Without Plugin in WP

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.

Placing Adsense Code in between 2 Posts

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!

Now let’s move on to the second segment where you will learn 2 methods to embed your Advertorial banner in between your Post content i.e. between the article body. The first method will be by using a code snippet similar to that shown above. The second method will be using ‘shortcodes‘, a recommended method. Just keep reading.

Placing Adsense Code in between Post content

Like I said before, we will be using 2 different techniques to implement the solution for this. The end result would be similar as shown in the screenshot below:

Adsense between article para

The first method has got a limitation and an advantage as well. The advantage is that all this code will be in action in all the pre-existing articles. The limitation is that – Every time you post something new, the advertorial banner will be displayed at the specified fixed location [i.e. either after 1st paragraph of the article, 2nd or 3rd or nth para].


1) We require to modify the functions.php file of the present theme. It is usually located in “your-blog-root-directory/wp-content/themes/your-current-theme-folder/

2) Paste the following code snippet anywhere in the file, preferably at the end.

<?php //Insert AD after second para of the post 
add_filter( 'the_content', 'post_ads' ); 
function post_ads( $content ) { $ad_code = '<div>Paste the Ad Code</div>'; 
if ( is_single() && ! is_admin() ) 
{ return ad_after_para( $ad_code, 2, $content ); } 
return $content; } // Function that makes this Work 
function ad_after_para( $insertion, $paragraph_id, $content ) 
{ $closing_p = '</p>'; 
$paragraphs = explode( $closing_p, $content ); 
foreach ($paragraphs as $index => $paragraph) { 
if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } 
if ( $paragraph_id == $index + 1 ) 
{ $paragraphs[$index] .= $insertion; } } 
return implode( '', $paragraphs ); }

3) Replace “Paste the Ad Code” with your banner code. Replace the number ‘2‘ in the following line to the number of your choice. It denotes the paragraph number after which you want the Advertisement to be displayed.

return ad_after_para( $ad_code, 2, $content );

4) That’s it. You are done.

We have got another method to overcome the limitation that we came across in the above method. For this, we will be implementing the ‘Shortcodes‘. It is simple, really easy to use and you can use it wherever you want in the post. Use this only if you have ignored the method given above.



1) Open the functions.php file located in “your-blog-root-directory/wp-content/themes/your-current-theme-folder/“.

2) Paste the following code at the end of the file:

function postads() { 
return '<div id="adsense"><center>Your_Adsense_Code</center></div>'; 
add_shortcode('myad', 'postads');

3) Replace “Your_Adsense_Code” with the code of your banner. Save the file and exit.

4) Now you can use the shortcode – [myad] wherever you want the advertisement to be displayed. You can also change the “myad” part to the name of your choice which is easy to remember.
For example: To obtain something like this [click here], the code will be inserted in the position shown below:

adsense implementation

I have used “adsense” as the shortcode. That is the reason you can find it as the shortcode in the above image. This is simple, isn’t it? You can use it wherever you would like the Adsense to be displayed unlike the above method where the positioning is fixed.

This is the method that I am currently using and I would recommend it to all. But in case if your blog has got over 100-200 posts already, then you could consider implementing the first method given above.

With inputs from sources 1,2,3


  1. Pritam August 20, 2013
  2. Rudd August 22, 2013
  3. Sadi August 27, 2013
  4. wpcoke August 28, 2013
  5. Ferretti October 12, 2013
  6. dave December 9, 2013
  7. Javier March 3, 2014
  8. Prateek May 10, 2014
  9. Abdul Wali September 25, 2014
  10. Aamit Wraj October 30, 2014
  11. Song Lyrics December 19, 2014
  12. jose marin June 26, 2015

Leave a Reply

Your email address will not be published. Required fields are marked *