Spiga

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 Posts by Categories



0 comments:

Post a Comment

Add to: Mr. Wong Add to: Webnews Add to: Icio Add to: Oneview Add to: Yigg Add to: Linkarena Add to: Digg Add to: Del.icoi.us Add to: Reddit Add to: Simpy Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Smarking Add to: Netvouz Add to: Folkd Add to: Spurl Add to: Google Add to: Blinklist Information