45 Display Elements only on Home, Item or Archive Pages in Blogger
June 11, 2008 /

Display widgets on pages in BloggerWhen 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 == &quot;index&quot;'>
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 &quot;index&quot; section in the above example with either &quot;archives&quot; or &quot;item&quot; 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 == &quot;archive&quot;'>
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 == &quot;item&quot;'>
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 == &quot;index&quot;> 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 == &quot;item&quot;'>

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 != &quot;item&quot;'>

The closing </b:if> tag should remain the same.

To clarify:

In the examples above,

== &quot;[page-type]&quot; (with two equals signs)

This means "If this IS the specified page type, display the content"

!= &quot;[page-type]&quot; (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:

11 June 2008 22:42 Jos said...

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?

12 June 2008 03:38 monaco said...

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.

12 June 2008 07:31 Ari Herzog said...

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.

12 June 2008 11:00 template-godown said...

this what i am looking for.

thanks amanda.

12 June 2008 14:47 Kazi said...

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:)))

13 June 2008 04:57
Anonymous said...

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?

13 June 2008 05:16 Mizake said...

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?

14 June 2008 00:33 prof_nona21 said...

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

16 June 2008 20:50 Amanda said...

@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?

17 June 2008 03:00 monaco said...

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?

18 June 2008 01:56
Anonymous said...

Hi Amanda,

Thanks for the reply. My blog template is minima.

Appreciate a code to get rid of the lines.

21 June 2008 16:46 fauzan said...

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..

23 June 2008 20:56 Stella said...

Thanks for this post, Amanda. I asked you the same question by email on how to this with SmoothGallery.

Well explained tutorial ;)

24 June 2008 10:18 Blogger-Holic said...

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 :)

26 June 2008 05:44 Aneesh said...

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>

6 July 2008 19:31 The Complex Days said...

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

20 July 2008 19:59 Mattijs Mooimuziek said...

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)

28 July 2008 23:15 tata said...

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

12 August 2008 03:36
diane said...

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!

12 August 2008 09:32 Amanda said...

@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.

3 September 2008 00:11
Anonymous said...

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!!

3 September 2008 03:11
Anonymous said...

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!

3 September 2008 04:38 Christian Biggins said...

Just what I needed - thanks!

14 October 2008 00:22 Aaron said...

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.

5 November 2008 22:05 Rei Zerburnn said...

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!

2 December 2008 07:06 DirtDigger (Tessa) said...

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

9 December 2008 19:17 PseudoRandom said...

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?

9 December 2008 22:43 DirtDigger (Tessa) said...

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

10 December 2008 05:10 DirtDigger (Tessa) said...

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

19 December 2008 11:49 Professional Career Guide said...

We can't directly do this by placing the any code on 'add element' html/javascript gadget?

3 January 2009 21:43 About Go Eggless said...

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.

6 January 2009 01:48 Todd @ Home Construction Improvement said...

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?

7 January 2009 18:50
Anonymous said...

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!

19 January 2009 14:54 Mike Rossi said...

This its very interessant post,nice day!

4 February 2009 02:49 Calculus Entropy said...

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

21 April 2009 02:26 Taylor said...

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

15 May 2009 16:43 Dr.Vishaal Bhat said...

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.

19 May 2009 17:52 Robert Almares said...

Great info in the post and in the comment section. I will be sure to bookmark this page. Thank you all.

8 July 2009 05:26 amplified said...

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...

29 September 2009 20:07 विनय ‘नज़र’ said...

<b:if cond='data:page.type == &quot;archive&quot;'>
Welcome to my blog!
</b:if>

how to access archive page?

7 October 2009 17:36 LikeItSpicy said...

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.

22 October 2009 18:50 Charlotte said...

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

1 November 2009 10:57 neilhoja said...

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..

12 December 2009 19:18
Anonymous said...

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.

9 June 2010 14:39 Blogger Templates said...

uff, i was running a lot for it, thanks my friend...

Post a Comment

Browse through the Archives

All existing posts are still available to view while I'm working on the site, albeit seen in a much simpler interface. Feel free to browse through the archives to find tutorials, templates and articles to help you build a better blog:

Blog Archive

© Blogger Buster 2010 Home | About | Contact | Hire Me