When designing your Blogger template, you may prefer certain elements to appear only on certain types of pages.
If you would prefer certain widgets or elements of your design to appear on the home page, but not on individual post pages, for example, you can use the "b:if cond='data:blog.pageType'" function to control this.
In this tutorial, I'll explain how you can easily control page elements to display (or not) on certain types of pages in your Blogger powered blog.
To display elements of your layout on certain page types, you will need to edit your blog's template code.
Be sure to make a full backup of your existing template before attempting to use the information in this tutorial!
To make a backup of your template, go to the Edit HTML page in the Layout section of your blog's dashboard. Then simply click on the "Download full template" link near the top of the page.
This will enable you to save your template as an XML file to your computer, which you can use to restore your template if you accidentally mess things up!
How the layout tags work
The <b:if> function tells Blogger we are setting conditions for the following section of content.
To control whether elements are displayed for a particular page type (ie: index, archive or item pages) we add the data:blog.pageType tag to the b:if statement, and complete this by stating which type of page where we would like this content to be displayed.
After this tag, we add the code or content to be displayed. Then finally we close of the b:if conditional statement with a </b:if> tag.
Trust me, this is not as complicated as it sounds! Let me give you an example.
If I wanted to display a "Welcome to my blog" message only on the home page, I could use the following section of code:
<b:if cond='data:page.type == "index"'>
Welcome to my blog!
</b:if>This code roughly translates as:
If this is the home page, display my "Welcome to my blog" message.
If this isn't the home page, don;t display anything here!
When we can translate the code into something more understandable, it becomes easier for us to use!
The three different page types
In the example above, I wanted to display my "Welcome" message on the home (or index) page.
In total, there are three page types for which you can use a similar statement:
- index (the home page)
- archives (to be displayed when readers view your archive pages)
- item (the individual post page)
We can replace the "index" section in the above example with either "archives" or "item" instead if we prefer to display only on archive or item pages.
To clarify:
If I want to display my welcome message on archive pages, I would use this section of code:
<b:if cond='data:page.type == "archive"'>
Welcome to my blog!
</b:if>If I want to display this only on item pages, I would use this code instead:
<b:if cond='data:page.type == "item"'>
Welcome to my blog!
</b:if>In your own blogs, of course, you may not wish to use this function to display a welcome message! In place of "welcome to my blog", you can insert any type of HTML code you like, such as an image, a hyperlink, and even an entire widget as we will explore below.
You can also wrap existing template code inside the b:if statements: simply type the first line above the section of content, and type the closing </b:if> tag directly below.
How to display widgets on certain page types
When we come to look at how widgets can be displayed on certain page types, the method is slightly more complicated (but not much!).
We cannot wrap an entire widget section in the b:if tags:
Instead, we must check the "expand widget templates" box near the top of the screen, and place the b:if tags just inside the b:includable tags which appear in the expanded widget template code.
Here is a diagram which displays where to add the two b:if lines to make a widget display only on the home page (click on the image for a larger version):
To clarify:
Place the initial <b:if cond='data:blog.pageType == "index"> tag just after the opening <b:includable id='main'> tag.
Place the closing </b:if> tag just before the closing </b:includable> tag.
Note: if you paste the sections of conditional code in the wrong place, you will receive an error message and cannot save your modified template code.
The "if not" statement
Let's say I wanted to display my "Welcome to this blog" message on all page types except for post pages. It would be much easier to say "if this is not an item page, display my message" than to use two seperate statements for archive and index pages.
For this, we can use (what I call) the "if NOT" tags.
In the examples above, you will notice the page type statement contains two "equals" symbols before the page type, like this:
<b:if cond='data:page.type == "item"'>The doubling of the equals symbols basically tells Blogger:
If this page IS this particular type of page, display the following code.
To tell Blogger to display a section if the page is NOT a particular page type (for example, if the page is NOT a post page), we can substitute the first equals sign with an apostrophe, like this:
<b:if cond='data:page.type != "item"'>The closing </b:if> tag should remain the same.
To clarify:
In the examples above,
== "[page-type]" (with two equals signs)
This means "If this IS the specified page type, display the content"
!= "[page-type]" (with an exclamation mark followed by one equals sign)
This means "If this is NOT the specified page type, display the content.
Note: these examples are just a part of the complete <b:if> template tags; for the entire conditional page type function to work, you will need to use the complete tags, as referenced above.
There are some limitations...
Unfortunately, there are some limitations when using page conditional tags in Blogger templates which I haven't yet been able to work around:
The "index" page type also functions for search pages and label pages.
Blogger's help center does state that one of the page types is "main", which refers to the home page.
However, using "main" in the pageType statement has never worked for me. Instead, we can use the "index" page type (not referenced in Blogger's help pages for some strange reason...), though this will also affect the search pages, which includes label pages too.
We cannot display elements only on certain page URLS
A few readers have asked if it is possible to display sidebar widgets only on certain post pages (which would be rendered by their unique URL).
I have tried many combinations of template tags, HTML and JavaScript code to accomplish this, but have had no success with this so far.
I have a feeling that Blogger may soon introduce password protected pages (since this is on the current wish list for user voting), though until such template tags are introduced, we are unable to display template elements for certain post URLs.
If anyone has successfully coded either of these two functions, please do get in touch to let me know how it may be achieved in Blogger!
Conclusions
Using the conditional page type function in our templates, we can choose to display widgets and other blog content only on index, archive and item pages. We can also choose not to display specified content on these page types.
I hope this tutorial has offered a good overview of how the b:if and data:page.type template tags can be used in your Blogger templates. Please let me know if this has been useful for you, or if there is anything you would like explained in more detail by leaving your comments below.





45 Comments:
Looks like a very useful addition. One I will certainly try out.
Thanks Amanda!
Have you ever thought about a making a better 404 error page for blogger or is that impossible?
Though it looks complicated, I will give it a try. I have often envied bloggers using WP who could display different widgets (adsense) on their pages.
Thanks for this Amanda.
I successfully switched to Amanda's Blue Steel template yesterday which has the conditional statements built-in.
Click over to www.ariwriter.com and you can see how some widgets are on the front page and others on the post page.
this what i am looking for.
thanks amanda.
hi amanda,
thx for sharing this information.can u explain how u made ur homepage? i mean after 1 post i wannna create homepage.
i hope it's clear to u what i mean:)))
Hi,
Thanks for the useful info.
I tried and it is succesful, however, the dotted line will not go together with it.
THe dotted line will still remain there and it is not nice at all. How do I remove the dotted line?
Hi Amanda, How can I say to imaging you? You are excellent one so that why I made a blogroll to your in my special space. Would you visit mine?
couldn't do it:(
can you help me i only want to put pic in home badge only i try the code u writ but nothing happen thanks
@Jos: As far as I know, it isn;t possible to modify the 404 page for layouts templates (though Classic FTP blogs are able to create a separate 404 page).
I am now wondering if we can change the message which appears when there are no entries for search results though. If I find anything useful, I will be sure to post it here.
@Monaco: I'm happy to say there are very few things available to WP users which we can't somehow use in Blogger too.
Hope this customization will become useful for you!
@Ari Herzog: I use conditional statements in a few of the templates I designed, but as you pointed out, it's most noticeable in the Blue Steel template :)
Thank you for your kind comments!
@Template-Godown: Glad to know this article will be useful for you!
@Kazi: The homepage I use now was very complicated to achieve. I used many conditional statements for DIVs and widgets beneath the posts (also in the sidebar too).
If you're just starting out with conditional statements and blog design, I would suggest you download the Blue Steel template to use in a text blog. This will give you some useful ideas about how the "magazine style" of template can be made to work, and can also be customized to suit your own needs.
Hope this will be helpful to you!
@Anonymous: I think your problem may occur because the CSS section of your template adds a line to the ".sidebar .widget" classes, rather than ".sidebar .widget-content" (in which case the line should disappear).
If you can let me know which template you are using, I may be able to tell you the exact code to change to prevent the line appearing where it isn't needed.
@Mizake: Thank you for stopping by and for your lovely comments :)
I had to bookmark your great blog design as it strikes me as being so unique!
@Prof_nona21: I'm not entirely sure what you need to do here. Do you need a widget to display in the header on certain pages, or is the problem something different?
I keep getting this error posted on my item page after trying this hack:
TEMPLATE ERROR: Invalid data reference page.type: com.google.layouts.framework.widgetview.GoogleMarkupException: No dictionary named: 'page' in: ['blog']
What did I do wrong?
Hi Amanda,
Thanks for the reply. My blog template is minima.
Appreciate a code to get rid of the lines.
sorry amanda, i cant understand the whole tutorial you are writing.. hope i can have more simple explaination from you..(",)
you write too many words..
Thanks for this post, Amanda. I asked you the same question by email on how to this with SmoothGallery.
Well explained tutorial ;)
hi Amanda,
did you know we can display widget using this tag:
<b:if cond='"[Insert URL Here]" == data:blog.url'>
</b:if>
change [Insert URL Here] with your unique URL. i found this at stubborn-fanatic. Hope this can help you :)
Hi amanda,
you can get rid of those limitations too..
To display something on a particular post page use
<b:if cond='data:blog.url == "The_Post_
Page_Url"'>
Your widget code
</b:if>
To display on Homepage alone use this condition
<b:if cond='data:blog.url == data:blog.homepageUrl'>
Your widget code
</b:if>
Hi,
the article is very well written and we've tried also the solutions offered in the comments.
Blogger keeps displaying this message:
"Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
XML error message: The value of attribute "cond" associated with an element type "null" must not contain the '<' character."
We're working with a minima black template which already features the includable tag.
Help.
Thank you
Hi Amanda,
In your post 'Change the Title Tags for your Blog' (May 11, 2008) you explain how to show each post's title in the browser's title bar for SEO optimization. Based on this customization, I found it's possible to have a widget display on the basis of each post's title.
Try this:
<b:if cond='data:blog.pageName != "your page's title"'>
your widget code
</b:if>
I have it working on my blog!
Thanks a lot fot a great blog and some wonderful tips. You have certainly been my teacher for the past eight months!
Cheers!
Mattijs
mooimuziek.blogspot.com (Dutch)
You use two codes when you should only use one. It appears to be a typo, but it's causing me some hell. Might wanna consider fixing the original post.
data:blog.pageType
and
data:page.type
Hi Amanda. I'd like to ramp this up a notch and apply it to one of my three sidebars. So far my attempts haven't worked (I tried your magazine-style tutorial as well. It worked as you laid out but not for this sidebar application). Thoughts?
I have a 3-column blog that I'd like to have the main column expand to take up more width on every page but the homepage...i.e. hide the 3rd column.
I think I'm missing something in the naming of the sidebar to make everything smooth.
Thanks!
@tata: Thank you for pointing this out. I'll go and fix the post now :)
@diane: That would be a great idea for Blogger design. It's something I've considered but haven't yet tried.
My first thought is that you would need to add the conditional elements in the style section of your template. For example, use the condition for item pages to increase the width of the main column and set the entire sidebar column to display:none.
In theory this should work :) Let me know if you need any advice about this.
this is not working for me (maybe b/c i used your SEO title trick??)
when i copy the first code you have posted here with the welcome to my blog message, it shows up on every page....not on the home page only
any help would be great!!
i was able to get the hide widget trick to work, but i still cant get the hide text/code one to work.
here is what ive tried:
...cond='data:blog.pageType != "item"'>
test
..b:if...
^^^doesnt work
...if cond='data:page.type != "item"'>
test
..b:if...
^^^also doesnt work
the reason i dont want to use the widget trick is b/c as one user already pointed out, the dots under the widget still show (i am using a customized version of minima) and i was hoping to use your trick to REPLACE the html code with a shorter one when in post (item) page.
thanks again!
Just what I needed - thanks!
Hi. Does anyone have any ideas on how to do this for Wordpress? My dashboard doesn't have a specific "expand widget templates" section.
Any help would be appreciated...one of my advertisers wants their text ad only displayed on the front page (!) and I can't do it.
This is exactly what I need to do for my blog, I want pages like About and Links to only display on their item pages and not on the main page or in archives.
But I'm not quite understanding, do I use the permalink of the post? Where do I put the code?
Thanks!
I really want to use this, but I too am unclear as to where to put this code. Could you please enlighten us, Amanda? :)
Tessa
Hi Amanda,
I am using a modified Minima template and I think someone has mentioned this before - the fix does work, but the separator line doesn't disappear. Is there a way around this?
Amanda,
I got it working. Now all I need is to find the section of code in the CSS that adds that line so I can delete it. I'm using a private test blog right now- could you post a line for me to search for so I can get rid of it? Or I could invite you to take a look? (not sure how to do that:)
Thanks,
Tessa
Just a note to all using this- to get rid of the lines that are left behind-
Look for border lines in the widget code and delete them to get rid of the lines :
.sidebar .widget, .main .widget {
border-bottom:1px dotted $bordercolor;
margin:0 0 1.5em;
padding:0 0 1.5em;
}
Like the border-bottom line in above code.
Hope this helps someone. Vin over at The Blog Doctor helped me with this.
Tessa
We can't directly do this by placing the any code on 'add element' html/javascript gadget?
Hello, I was wondering if there is a way to have my widget on all pages except for the label page that you get when you click on a label. Does this fall under the limitations of blogger that you mentioned? Thanks.
This works really well with one exception. For some reason when I use this on a FEED widget it crashed IE. Works fine in Firefox but it crashes the post pages in IE. Any suggestions?
nice post! tnx Amanda
but what I was really looking was how to remove gadget from all other pages, post... etc by keepin only on "homepage"
and I found it in your coments
@ Aneesh tnx a lot!
This its very interessant post,nice day!
I'm trying to remove my reactions only on the main page, but the code is not working. Any thoughts? It either doesn't work at all, or it removes the item from all pages, main, item, and archive. I'm using summaries for my posts on the main page. Could that have something to do with it?
I'm using the Minima template.
Thanks!
Calculus
You can display items only on certain URL pages... That's easy. Lol I used to use that technique over the "archive" technique. My code got way too large though lol
Hi,
Thanks for the wonderful tutorials on this blog. I actually had a small request. Could you please tell me, is it possible to make a template, such that there are three columns on the front page, labels and archives, but only two columns on the content page? Also is it possible that the content can take up the unused space? Will be waiting for your reply and any suggestions u can make.
Great info in the post and in the comment section. I will be sure to bookmark this page. Thank you all.
Hey, this is really great..just one question. Is there a way to hide the blog posts on the homepage and only display it on the post pages? I've tried using this method on the element but it won't work...
<b:if cond='data:page.type == "archive"'>
Welcome to my blog!
</b:if>
how to access archive page?
Hi Amanda:
This fix does work, but the separator line doesn't disappear when the widget disappears from the page. Is there a way around this?
I am using the Minima Ochre template.
Had some errors,
"Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
XML error message: The value of attribute "cond" associated with an element type "null" must not contain the '<' character."
under: To clarify: a ' was missing
[code]
== "index"> tag just after the opening
[/code]
the good code
[code]
== "index"'> tag just after the opening
[/code]
now it works fine Thx
hi amanda.. thanks for this great tuts..
but, i try more and more, still couldn't do anything with the followers widget.
could you help me? i wanna show the followers widget only on item.
hope you pleased to visit my blog, and give a word. thanks..
Thank you very much for post such clear and valuable information.
However, I only got the function to work when using this html:
...cond='data:blog.url == data:blog.homepageUrl...
I'm not sure why the others in this post don't work for me, but at any rate, this version seems to do the trick just fine.
uff, i was running a lot for it, thanks my friend...
Post a Comment