2

Many Bloggers are asking me on what code/css did I use to put ads in the middle of my posts. Well I have my post before about putting adsense ads anywhere in the posts but that is manually adding of <!-- adsense -->.  Now I will show this code that  will add the adsense ads automatically. As I research about this there are some posts about his and one is in Ajbanda article of showing ads in the middle of post, He created a code that will look for  <br/> tags on the articles, this one just simplified version of the code

How To Put Adsense In The Middle of Post Automatically

First things first: Must take backup of your template before making any change.
( Tips: Use the Asynchronous Adsense Code for Maximizing Page load)

1. Find <head> tag in your blogger template and place latest jquery just below it. Please avoid to use two jquery links, remove older one and use new version.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" ></script>

2. Second look for this code <data:post.body/> inside your template and replace it with the following code -
<div id="PostBody">
 <data:post.body/>
</div>

<div id="AdCode">
<div style='margin:5px 0;text-align:center;clear:both;'>
 <!-- Ad Unit Code Here -->
</div>
</div>

New Blogger template may have 2 or 3 <data:post.body/> code. You should have to identify correct one. Generally 2nd instance of <data:post.body/> is right code to replace in new blogger templates.
3. Now search for </body> tag inside your template and put following javascript before/ just above it.
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<script>
 $AdCode = $("#AdCode").html();
 $("#PostBody br:lt(1)").replaceWith($AdCode);
 $("#AdCode").remove();
</script>
</b:if>

4. Save the changes in Blogger template and have fun.

You can choose your Ad location

By replacing the value br:lt(1) with br:eq(n), where n= 1, 2, 3… which is number of line breaks after which Ad will appear in your post. e.g. If you want to show Ad after 5th line break then replace br:lt(1) with br:eq(5) and you are done.

Before placing the Ad codes be sure to Parse the adsense codes. You can use my parser: HERE
As I read on the adsense policy there are no specific about this one and I think this is just placing the ads inside blog post like placing it below post title which is allowed.
You can also read their Policy. Final choice is yours, you will use it for your blog or not.


Post a Comment

  1. Thank you very much for the tutorial helped me a lot to set up my blog

    ReplyDelete
  2. Hi Again, I used br:eq(n) and it is now showing an extra ad at the end of my posts too. Can you help figure me out why this is so?

    ReplyDelete