Sunday, April 22, 2012

Using Conditional Tags in Blogger

Conditional tags in Blogger are used to tell browsers what to do in certain conditions. It is a great way to do certain things like hiding widgets or gadgets or your share buttons in Blogger. For example, when we use:

<b:if cond='data:blog.pageType == &quot;item&quot;'>
Hello world
<b:else/>
Goodbye Cruel World
</b:if>
 
 
It gives the browser the condition that when it’s an item (post) page then show “Hello world” otherwise, for every other pages show “Goodbye Cruel World”. Interesting, isn’t it.
A conditional tag comes handy when you are designing your Blogger blogs and customizing and editing it from the scratch to take it to the next level. But first you need to know how many types of pages are there in Blogger and then how to use the conditional tags respective to that type of page. In this tutorial I will try to cover most of it, so let’s start.


Type of Pages in Blogger and Conditional Tags to Use

Item Page

It refers to every other page except item (post) pages. The conditional tag:

 
<b:if cond='data:blog.pageType == &quot;index&quot;'>
Hello World
</b:if>
 

Archive Page

It’s for your archive pages in Blogger. The tag will be:

<b:if cond='data:blog.pageType == &quot;archive&quot;'>
Hello World
</b:if>
 

Static Page

For static pages in Blogger it will be:

<b:if cond='data:blog.pageType == &quot;static_page&quot;'>
Hello World
</b:if>
 

For a Particular Page

If you want to do something on a particular page then the conditional tag for it will be:

<b:if cond='data:blog.url == &quot;URL of the page&quot;'>
Hello World
</b:if>
 
The only thing that you have to do is replace “URL of the page” with your
page’s URL which you want to have. 

Homepage only

It's for the homepage only.

<b:if cond='data:blog.url == data:blog.homepageUrl'>
Hello World
</b:if>
 

Alternative Condition

Now suppose you want a conditional tag that alternates every rule. Say, in first example, I showed how to display something only in post pages but just an “!” mark can alternate that condition. For example, when we apply:

<b:if cond='data:blog.pageType != &quot;item&quot;'>
Hello World
</b:if>
 
It will set the condition that it should be shown on every page other than post
page. 

 


 
 

 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...