How To Run Custom Code On Selective Blogger Posts

Often we want to display ads on selective posts. By default, once the ad code has been added to blogger template, it appears on every single blog post. Suppose, we do not want to show ads on About, Contact and Archive pages. Or we want that posts having more than 10 comments should get image banner ads instead of text ads. Suppose you have a multi author blog, and you want that every author runs its own ad code on the post written by them. Or you may want that posts with tag 'Software' display no ad or only display only image ad. How to do all this? Well, here is the solution.

Display Author's Own Ad Code On His Post


Suppose, there is a multi-author blogger blog owned by Alex, Peter and Melinda. They all have Adsense accounts and want to display their own ad code on the posts written by them. How to achieve this? Let's see...

The following code will run each author's own ad code on the post written by them. In this example, the ad will appear at the end of post body. If you want to display the ad just below the post title shift the if...else section above the <data:post.body/> line.


<data:post.body/>
<b:if cond='data:post.author == &quot;Alex&quot;'>
/* Put Alex's Adsense Code or Custom HTML/JavaScript here */
<b:else/>
<b:if cond='data:post.author == &quot;Peter&quot;'>
/* Put Peter's Adsense Code or Custom HTML/JavaScript here */
<b:else/>
/* Put Melinda's Adsense Code or Custom HTML/JavaScript here */
</b:if>
</b:if>


Run Custom Code On Selective Posts


Let's take one more example. Suppose, we decide not to show any ad on About, Contact and Archive pages. Or we may decide to run some custom code instead of Adsense ad on these pages. To achieve this, our template code must identify the correct page while reader is opening it in the browser. How to achieve this? Let's see...

Every post in blogger has a unique post ID associated with it. This post ID is generated when the post is published. Drafts do not have any post ID associated with them. First open your post in blogger editor, in our example, it's Contact page. Now see the URL in your browser's address bar. You will find the post ID at the end of the URL, as shown in the image below.

Blogger Post ID

Copy this code in notepad and close the post. Now add the following code in your template.


Note: Replace xxxxxxxxxxxxxxxxxxx with your post ID



<data:post.body/>
<b:if cond='data:post.id == &quot;xxxxxxxxxxxxxxxxxxx&quot;'>
/* Put Custom HTML/Javascript Code here */
<b:else/>
/* Put Regular Adsense Code here or leave it blank to show no ad */
</b:if>


Run Custom Code on the Basis of Comments


Here is one more possibility. Let's say, a blogger may want to run custom code on posts having no comments or having more than 5 comments. On the other hand, he may want that posts having comments between 1 to 5 display regular Adsense ads. How to achieve this? Let's see...

Possibilities and combination's can be infinite. I just took an example to explain the trick.


<data:post.body/>
<b:if cond='data:post.numComments == 0'>
/* Put Custom HTML/Javascript Code here */
<b:else/>
<b:if cond='data:post.numComments &gt; 5'>
/* Put Custom HTML/Javascript Code here */
<b:else/>
/* Put Regular Adsense Code here or leave it blank to show no ad */
</b:if>
</b:if>


Run Add On Custom Code Based On Post Tags (Categories)


Here is one more example. Suppose, you want to run custom code on all those posts having 'Software' tag (Category) associated with them, in addtion to your regular Adsense code. How to achieve this? Let's see...


Note: Replace Software with your Tag (Category)



<data:post.body/>
<b:if cond='data:post.labels'>
<b:loop values='data:post.labels' var='label'>
<b:if cond='data:label.name == &quot;Software&quot;'>
/* Put Custom HTML/Javascript Code here */
<b:else/>
</b:if>
<b:if cond='data:label.isLast != &quot;true&quot;'/>
</b:loop>
</b:if>
/* Put Regular Adsense Code here or leave it blank to show no ad */


NOTE: You can shift these codes to appropriate place in the template according to your needs.

Related Post


How To Embed Adsense Ads In Blogger Posts

If you enjoyed this article, get email updates (it's free).

blog comments powered by Disqus

NEXT POST: Newer Post PREVIOUS POST: Older Post Home
 

About Rajeev Edmonds

MintBlogger author - Rajeev EdmondsRajeev Edmonds has been blogging since 2006 about digital marketing, social media and of course about problogging. He specializes in WordPress installation and Thesis theme customization. He is the founder and editor of MintBlogger where he blogs about new media strategies.

Since April, 2012, he's making his living entirely from his blogging efforts. He holds a post graduate degree in computer applications and loves to dig in CSS/HTML code. Read more →

Get Custom Design For Your Blog

Website showcase Nothing converts better then a unique blog design with a branded look n' feel.

Get a unique design for your blog that brings more leads and prospects for your business. Give a new skin to your existing theme to stand out in the crowd. Change theme and see the difference!

Request a Free Quote NOW!

Start Your WordPress Blog

WordPress logo Are you planning to start your own self-hosted WordPress blog? You can get one today!

This is right time to get started. Claim complete control over your WordPress blog hosted on a reliable and powerful web server. Be the owner of your WordPress blog without any restrictions.

Claim Your Free Blog Setup NOW!