Conditional Tags - Targeting Specific pages In Blogger

You may ever need to display any blogger widget or adsense ads unit in one (or more) specific page(pages) such as home page,item or post page,index or list page,label page,yearly archive page,static page,or a specific page using its url this can be done using conditional tags in blogger. 

Today i am going to explain you the diffrent conditional tags along with their implementation in your blogger template to show specific widget or adsense adds on the specific pages according to your need.



General syntax of conditional tag is as follows:
<b:if cond='YOUR CONDITION HERE'>
Your task to perform
</b:if>
In above syntax you can use different conditions (I am going to explain each bellow) if your condition is found true then only the task written in the code will be performed and if your condition is found false then task will be skipped.
Now lets discuss some conditions that we need to use frequently. 
  • How to display any blogger widget or adsense ads unit only in homepage

Now for performing this task we need to setup a conditional tag as follows,
<b:if cond='data:blog.url == data:blog.homepageUrl'>
 Your widget code or your adsense ads code here
</b:if>
Now what is the meaning of above code?

In above code we used condition 
cond='data:blog.url ==data:blog.homepageUrl' 

The meaning of this condition is that the task to be performed that is the text written above in red colour ,will be only performed if the current page is a homepage i.e. your blogger widget or adsense ad unit will only displayed on homepage.

There are two possiblities about using this code as follows,

1)When you want to add any blogger widget code directly in template then you just have to change your code like this, 
<b:if cond='data:blog.url == data:blog.homepageUrl'>
Your original widget code or your adsense ads code here 
</b:if>
And you are done.

2) Suppose you have a widget in your sidebar and you have to display it only on the homepage, Now firstly you need to find the location of the code in your template, How will you find?

Here I tell you a simple trick to find any widget code location in blogger template and apply conditional tags.

This is my facebook like box situated in my sidebar.


Now we will find its code location in my blogger template and then we will apply conditonal tag to it to show it only in homepage.

Steps: 

� Go to blogger dashboard click on design and add a title to the widget. I have given it title Ritesh009.



� Now click on edit html and then check the box expand widget templates.

� Now search Ritesh009 ( press ctrl+f to search) ,this will give you the location of the code of the widget now you can add conditional tag to this code as shown bellow,



And done, now the facebook like box widget will be shown only on the homepage.

This was only a example you can use this technique to design your blogger template like a problogger.

Different conditional tags are as follows, 
  • For item pages i.e post pages: 

<b:if cond='data:blog.pageType == "item"'>

  • For index i.e. list pages: 
Index pages includes homepage,labels page ,and your yearly archieve page. 
<b:if cond='data:blog.pageType == "index"'>

  •   For Archive page: 
<b:if cond='data:blog.pageType == "archive"'> 

  • For static page: 

<b:if cond='data:blog.pageType == "static_page"'>
  • For homepage 

<b:if cond='data:blog.url == data:blog.homepageUrl'>
  • For specific page(using its url as condition) 

<b:if cond='data:blog.url == "Put url of page here"'>

Some extra knowledge about conditional tags for probloggers:
  •  Adding alternative false condition using <b:else/> tag:
<b:if cond='YOUR CONDITION HERE'>
Task 1(will be executed if condition is true)
<b:else/>
Task 2(Will be executed if condition is false)
</b:if>
  • Reversing a condition using != operator:
<b:if cond='data:blog.url != data:blog.homepageUrl'>
Your widget code or your adsense ads code here
</b:if>
Now as we used != i.e not operator in above conditional tag, the conditon is reversed i.e now the widget will be displayed in all the pages except homepage.
Happy blogging :)