Targeting specific pages/URLs with conditional tags
Conditional tags are Blogger template tags that allow you to specify parts of your template to appear only under certain conditions. One particularly useful application of conditional tags is in specifying on which page or pages a HTML element should appear.
Ever wish you could display only relevant widgets onto a page, hide sidebars on certain pages, display different buttons on different pages, or apply a meta tag only to selected pages? Well, if applied properly, conditional tags can make all that happen.
I. Conditional tag syntax
The syntax is like this:
<b:if cond='PUT_CONDITION_HERE'> </b:if>
It is made up of a <b:if> tag, with a cond attribute added. Condition is entered as the value of the cond attribute. Each (opening) <b:if> tag need to be closed with a closing </b:if> tag.
II. List of conditional tags
Below is a list of conditional tags that target specific pages. I only list the opening tags here. Just make sure you include the closing </b:if> tag when applying a conditional in your template.
- Index (list) pages
Index pages include homepage, labels page and yearly archive page.<b:if cond='data:blog.pageType == "index"'>
- Post (item) pages
<b:if cond='data:blog.pageType == "item"'>
- Static pages
<b:if cond='data:blog.pageType == "static_page"'>
- Archive pages
<b:if cond='data:blog.pageType == "archive"'>
- Homepage
<b:if cond='data:blog.url == data:blog.homepageUrl'>
- Specific page/URL
<b:if cond='data:blog.url == "PUT_URL_HERE"'>
- Post and static pages
<b:if cond='data:blog.url == data:post.url'>
- Label-search pages
<b:if cond='data:blog.searchLabel'>
- First post
This is not a page, but a conditional for the first post. Used for targeting the first post on multi-post pages.<b:if cond='data:post.isFirstPost'>
III. Applying conditional tags
- To apply a conditional tag to a content, simply put the content between the opening
<b:if cond…>and the closing</b:if>, like so:<b:if cond='data:blog.pageType == "item"'> CONTENT (TO BE EXECUTED IF CONDITION IS TRUE) </b:if>
In the example above, the content will only appear on post pages.
- If you want to specify a alternate content (when the condition is false), you need to insert a
<b:else/>tag followed by the content, like this:<b:if cond='data:blog.pageType == "item"'> CONTENT 1 (TO BE EXECUTED IF CONDITION IS TRUE) <b:else/> CONTENT 2 (TO BE EXECUTED IF CONDITION IS FALSE) </b:if>
You can place the conditional anywhere in your template HTML, except inside a section or inside a widget content box. The content can be a div, a section, a style tag, another conditional tag etc.
- Reversing a condition
A condition can be reversed simply by replacing the comparison operator from
==(is equal to) to!=(is not equal to), like so:<b:if cond='data:blog.pageType != "item"'> CONTENT (TO BE EXECUTED IF CONDITION IS TRUE) </b:if>
In the example above, the content will only appear on pages other than post pages (i.e. removed/hidden from post pages). This method is not applicable to Label-search and First Post conditionals.
IV. Application examples
Below are some examples of what you can do with conditional tags:
- Display widgets on specific pages
- Applying different styling to static pages
- Adding description and keyword meta tags
Enjoy!
30 comments to "Targeting specific pages/URLs with conditional tags"
Thanx for this post. =D
I always wondered how to do this.
thanks, I've read one like this before and already implemented to my blog. But this one is more useful to make the blogspot blog looks more professional.
@I like Kung-Fu Movies!
My pleasure :)
@Affan Ruslan
You're right, conditional formatting, coupled with Blogger Template Designer can work wonders (provided you know what you're doing).
Hi, I've benefited from this article! I do have a question though. I'm trying to display a widget on ONE specific static page. The code above limits the widget to static pages... but ALL of them. I have 10. Is there a way to specify ONE particular static page to display a widget on? Would love to do this on my baking blog.
Dawn @ BeatUntilFluffy@gmail.com
I figured it out. I had to use #6. Thanks so much for posting this. It's easy to find things here as well. You know, the google search gadget doesn't work that great from what I've found. Is there a way to make your own that actually pulls up all related posts to a label and not just a few here and there?
@Dawn
I use Google Custom Search.
If you're interested, here's a tutorial for installing the search box in navigation bar, just like Blogger Sentral's.
How if I want ts to display for examples in index and tem only? Help please...
@Cenangau
This variation should work:
[b:if cond='data:blog.pageType == "index"']
CONTENT (TO BE EXECUTED IF CONDITION IS TRUE)
[b:else/]
[b:if cond='data:blog.pageType == "item"']
CONTENT (TO BE EXECUTED IF CONDITION IS TRUE)
[/b:if]
[/b:if]
thanks for this useful hack =D
but what if you like to display a widget NOT on a specific page, but on all other pages?
have you got an idea?
thx
leo
@Leo
"To reverse the condition, change the comparison operator from == (is equal to) to != (is not equal to)"
Thanks very much for the lesson as well as the code. I used #6 with some modifications.
I wanted to have a thank you page for those who used the paypal donate button. I did not want any widgets ontheat page especially a donate widget. I already have five widgets on my template and I did not want to do this for each widget and redo for future widgets so I found the ID for the sidebars and used the same code arrangement to hide them, which means I only had to do it twice.
Code here:
[b:section-contents id='sidebar-left-1']
[b:widget id='LinkList1' locked='false' title='Alternative News Sources' type='LinkList']
[b:includable id='main']
[b:if cond='data:blog.url == "http://rollinshultz.blogspot.com/p/thank-you.html"']
[style type='text/css']
#sidebar-left-1 {display: none;}
[/style]
[b:else/]
skipping on down to inserting the [/b:if] before the [/b:includable] tag.
I have succesfully trimmed of the sidebars and now I must find a way to hide the page and expand the center post body to full width. I am having trouble finding the control for the center section. I am using the awesome inc template.
@Rollin Shultz
There is a better/easier way of hiding the sidebar and widen the post column. I would suggest you use this method:
Applying different styling to static pages
however you want to replace the conditional tag in the tutorial with the one you posted above.
can I used operand AND / OR in If conditional statement?
thanks in advance! :)
@varisphere
I'm afraid you can't do that.
Hi,
It doesn't work for me. Can you check my code? I would like to widget to only show on a specific page. (i had to as a space in h2, style and div tags in order to post here)
#WidgetID {display:none;}/*to hide empty widget box*/
Many many thanks!
@Al
When posting HTML/XML code, replace < and > symbols with [ and ] respectively.
Hi,
I tried it again on a different widget and it worked. Here is the code:
[b:widget id='LinkList1' locked='false' title='Links' type='LinkList']
[b:includable id='main']
[b:if cond='data:blog.url == "http://myblog.blogspot.com/p/links.html"']
[b:if cond='data:title'][h2][data:title/][/h2][/b:if]
[div class='widget-content']
[ul]
[b:loop values='data:links' var='link']
[li][a expr:href='data:link.target'][data:link.name/][/a][/li]
[/b:loop]
[/ul]
[/div]
[b:include name='quickedit'/]
[b:else/]
[style type='text/css']
#LinkList1{display:none;}/*to hide empty widget box*/
[/style]
[/b:if]
[/b:includable]
[/b:widget]
Thanks again!
I want to highlight the links in my post with a Blue background color, for eg: links on (www.shoutmeloud.com) on post pages.
How do i apply blue background color for Only links in my post content ?
I tried using the conditional tag you specified above for post pages but it applies to all content including sidebars, which i dont want.
Thanks.
@Sagar
To target in-post links, use .post-body a selector instead of a.
Example:
.post-body a {background: blue;}
great tips. solve my problem. keep up the good work..
Is there any possible way to make it where you can target specific pages for this tactic? I couldn't find a way to get it to work on different pages.
Basically what I'm going for with the blog I have is that I want a sidebar of buttons (a nav tool) to pop up on any post I make featuring a certain item (in this case 3d Models) so that you can easily navigate to other models and such.
I have the sidebar popping up when you click on 3d Models, but then when clicking on each button to go to that model (which is an individual post) the sidebar disappears. I'd like to know how to target multiple posts with this code so that any post I create with my 3d models will let the sidebar show up, but the motion graphics/main page/animation pages will not have the sidebar.
Here is a link to the page:
http://jeremieldridge.blogspot.com/p/3d-modeling.html
(all of the images are pretty crude and are simply standins before plugging in new items)
For any more clarification please feel free to email me at eldridgejeremi@gmail.com
@J Eldridge
It's rather complicated but achievable, I think. Here's an idea:
1. Label the posts with a common label.
2. Detect the label, and style the page accordingly. This post: Creating static pages in Blogger will show you how to do that (in step 3).
3. Wrap your code in a post page conditional tag.
thanks a lot
I like your tutorial. I keep coming back to your blog and get reference. This one works like a charm on my blog. Million tQ!
You're missing two very important page type: label and search
My tutorial shows you how to customize ALL seven page types. Have a look: 7 Blogger Page Types: Analysis, Code Snippets, Data Matrix | MY STADY
@Alain-Christian
I didn't know about the search page type. Thanks for the info :)
i need complete sitemap like index for my blog posts. there are more than 4000 posts, i can't get it via any proper means.
help me create a sitemap code so that i can create a page with posts categorized by labels.
my blog here: www.mobilemoviesite.com
hope you help me resolve this mess.
Best Regards
@yblinx
Try this link:
http://www.abu-farhan.com/2010/05/table-of-content-for-blogger-new-style/
it is can be done with id element? i really really need your answer.
example..
[b:if cond='data:blog.url == "http://css3-jquery-html-lab.blogspot.com/#hide"']
[/b:if]
Using #hide .. Can it be done?..
@Muhd Firdaus
I don't think that will work.
If your question is unrelated to this article, please use our Facebook page.
When posting HTML/XML code, replace < and > symbols with [ and ] respectively. The reverse applies when copying code from my replies here.