Using Conditional Tags in Blog to Display Selective Widgets or Post Title
Some times Conditional Tags Proves very powerful.They can help a lot to alter our Template in our own what.For example there is java script running all pages of our blog.And we want show that script only on homepage or post page only, Then this can be easily done if we have knowledge of conditional tags.So lets start.
Before Doing Operations on your Template Please back UP it
Terms Which we are going to discuss :)
home page- I think you people know it very well
Archive Page- Refers to archive pages..
Item Page - Refers to Post Pages
Index Page - Includes home page + label page + search results page
Selective Display..
you can use the following conditions to check or display different items on the page.
If You want to display a Welcome message only on the home page ..
<b:if cond='data:blog.url == data:blog.homepageUrl'>
Welcome massage
</b:if>
Displaying something on all pages other than the homepage
<b:if cond='data:blog.url != data:blog.homepageUrl'>
i am everywhere except homepage
</b:if>
Displaying something only on the Archive Pages..
<b:if cond='data:page.type == "archive"'> I am on Archive Pages </b:if>
Displaying Item other than Archive Pages..
<b:if cond='data:page.type != "archive"'> i Don't like Archive page </b:if>
Displaying something on post page only
<b:if cond='data:page.type == "item"'> I am on a post page </b:if>
Displaying something on non post page only
<b:if cond='data:page.type != "item"'>
This is a non post page
</b:if>
Now displaying something on a particular url only (it can be a particular post page only or something like that)
<b:if cond='data:blog.url == "Page URL on which you want to display"'>
The Item to displayed(widget or something like that)
</b:if>
(you will have to replace Page URL on which you want to display with the url where you need the widget to be displayed..)
Now displaying something on all pages other than a particular URL
<b:if cond='data:blog.url != "Page_Url on which you don't want display item or widget"'> The item here will not be displayed on The_Particular_Page_Url </b:if>
Now how to use two conditions (or more).
<b:if cond='data:blog.url != "The_Particular_Page_Url1"'> <b:if cond='data:blog.url != "The_Particular_Page_Url2"'> this is not Particular_Page_Url1 and this is not Particular_Page_Url2 </b:if> </b:if>
the else thing..
What if you need to display something on the homepage and something else on all other pages.. here comes the use of if else
<b:if cond='data:blog.url == data:blog.homepageUrl'> This is the homepage line1 line2 <b:else/> This is not the homepage line 3 line4 </b:if>
Different combination's are possible..
Limitations
OR conditions can't be used
AND conditional statements can't be used
The main Disadvantage is that we can't enclose a whole widget code within the b:if tag.. otherwise we will get some message telling that a b:section can't have b:if s i.e you can wrap an unexpanded widget within a b:if condition
Wrapping a widget with the conditional tags
Every widget other than the Blog Post Widget has this general structure
<b:widget id='something' locked='' title='' type=''> <b:includable id='main'> somethings here </b:includable> </b:widget>
Wrapping conditional tags can be done as
<b:widget id='something' locked='' title='' type=''> <b:includable id='main'> <b:if cond='data:blog.url == "The_Particular_Page_Url"'> some things here </b:if> </b:includable> </b:widget>
this code will display the widget on The_Particular_Page_Url only (you have to provide this url)
Thx for the info!
ReplyDeleteI've updated my blog contact-page and looks perfectly now.
nice tut. well explained. looking forward for more awesome tuts. =]
ReplyDelete