Wednesday, October 15, 2008

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

Related Posts with Thumbnails
Want to make sure you always get the latest updates on MintBlogger.com? It's totally free! Sign up, and we'll send the articles straight to your inbox. No spam, ever. Period.

Free Updates by Email Free updates by RSS (What is RSS?)

How smart is your Theme?  How good is your support? Check out ThesisTheme for WordPress.

The Ultimate Premium Wordpress Theme

Building a website was never the same with new Thesis 1.6. The Thesis Theme for WordPress is a high-quality template system that you can use to improve your site immediately.
  • Strong SEO - Expertly-coded HTML + CSS + PHP framework makes Thesis the strongest engine to power on-site SEO. The core SEO settings are activated automatically, the moment you install thesis on your website. Just set it and forget it. Check out Thesis SEO features now!

  • Flexible Design Options - With Thesis in your hand, you don't need to hire a web-developer to design your website or blog. Thesis killer design options gives you complete control over every aspect of appearance, layout, color and typography. See Thesis Design Options now!

  • Strongest Support Community - Thesis is supported by thousands of talented users ready to help you in forums. Stack of excellent video tutorials makes it easy to understand every concept in no time. Grab Thesis 1.6 Now!
blog comments powered by Disqus