How To Insert Adsense Between Posts In WordPress Without Plugins

By

7 Comments Posted on August 18th, 2013

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!


Google Adsense WordPress

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:

Adsense between posts

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(); ?>

OR

<?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].

Instructions

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.

shortcodes

Instructions

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

7 Responses to “How To Insert Adsense Between Posts In WordPress Without Plugins”
  1. Pritam

    I have just applied for Google adsense hope I get approved :)
    This posts is awesome for newbie like me . Plugins funtionality may slow down the speed of our site .
    So I think its better to have less plugins as much as possible..
    Thanks for sharing ..
    Pritam recently posted…Best Online Resources To Learn Basic CodingMy Profile

  2. Rudd

    Awesome, the first method probably isn’t very practical but the 2nd and 3rd method are more useful and flexible.
    Rudd recently posted…How to Change the Order of WordPress CommentsMy Profile

  3. Sadi

    It was a life saving help Nikhil. I was looking for it. Thanks.

  4. wpcoke

    Can you tell me how to arrange two adsense ads side by side between post content?
    wpcoke recently posted…Disable Post Revisions in WordPressMy Profile

  5. Ferretti

    Great Post!! … Quick question – My main index is marked up like so:

    …. Wondering if there’s anyway to incorporate the first method into this since the php is all wrapped in one opening and closing tag?

  6. dave

    Hi Nick,

    Thanks for demonstrating ad methods in this post, I manage to get the ad in between 2 post to work on index.php but not on archive.php. Was wondering if you can please help me out, I’m using twenty thirteen wordpress theme.

    As you suggested the following two php tags

    are seperated unlike the index.php, I’ve try placing your code in different positions in archive.php, but couldn’t get it to work. Let me know if you can work this out thanks heaps.

    Regards
    Dave

    Following from twenty thirteen wordpress theme

  7. Javier

    I’m having problems to insert adsense code between 2 posts.

    Thank you for your help

    My index code is :

    ‘post’,

    ‘posts_per_page’ => 12,

    ‘cat’ => ‘-’ . $category_ID,

    ‘paged’ => ( get_query_var(‘paged’) ? get_query_var(‘paged’) : 1)

    );

    query_posts($args);

    $x = 0;

    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;

    if($paged > 1)

    $y = (0 + (($paged-1) * 12));

    else

    $y = 0;

    while (have_posts()) : the_post(); ?>

    <div class="home_post_box home_post_box_last" onmouseover="show_post_desc()” onmouseout=”hide_post_desc()”>

    <div class="home_post_box" onmouseover="show_post_desc()” onmouseout=”hide_post_desc()”>

    <!–<img src="/images/blog-image.jpg” />–>

    <a href="”> ‘post image’, ‘class’ => ’rounded’)); ?>

    <div class="home_post_desc" id="home_post_desc”>

    <?php if($x == 2) { $x = -1; /*echo '’;*/ } ?>

    <?php next_posts_link('’) ?>

    // Ajax-fetching “Load more posts”

    $(‘.load_more_cont a’).live(‘click’, function(e) {

    e.preventDefault();

    //$(this).addClass(‘loading’).text(‘Loading…’);

    //$(‘.load_more_text a’).html(‘Loading…’);

    $.ajax({

    type: “GET”,

    url: $(this).attr(‘href’) + ‘#main_container’,

    dataType: “html”,

    success: function(out) {

    result = $(out).find(‘#load_posts_container .home_post_box’);

    nextlink = $(out).find(‘.load_more_cont a’).attr(‘href’);

    //alert(nextlink);

    //$(‘#boxes’).append(result).masonry(‘appended’, result);

    $(‘#load_posts_container’).append(result);

    //$(‘.fetch a’).removeClass(‘loading’).text(‘Load more posts’);

    //$(‘.load_more_text a’).html(‘Load More’);

    if (nextlink != undefined) {

    $(‘.load_more_cont a’).attr(‘href’, nextlink);

    } else {

    $(‘.load_more_cont’).remove();

    $(‘#load_posts_container’).append(”);

    // $(‘.load_more_cont’).css(‘visibilty’,'hidden’);

    }

    if (nextlink != undefined) {

    $.get(nextlink, function(data) {

    //alert(nextlink);

    if($(data + “:contains(‘home_post_box’)”) != ”) {

    //alert(‘not found’);

    // $(‘.load_more_cont’).remove();

    $(‘#load_posts_container’).append(”);

    }

    });

    }

    }

    });

    });

Bloggercent is a Dofollow blog. Tick the 'Commentluv' button for your link to carry a "dofollow" tag.

Leave a Reply (Our Comment Policy)

CommentLuv badge