Thursday, October 28, 2010

Conditionally Displaying Template Objects

Some time ago, I wrote how to make a static home page, where the key element was an HTML gadget, tweaked to display only when the home page of the blog is being displayed. That's a useful tweak, and it's a tweak that can be used in various other conditional displays also.

If you know a little template XML code, you can construct alternate conditional statements, and have template objects display in any of several other specific conditions.

You'll probably recognise the first conditional, from the above example, which shows how to display a Welcome post on the home page.

<b:if cond='data:blog.url != data:blog.homepageUrl'> <style> #Text1 {display: none !important;} </style> </b:if>
As a second example, I'll offer my Labels list displayed on a static page. For a third example, I'll offer my expanded Followers page.

Any of the snippets shown below can be substituted for the conditional snippet, above - which is the one used in the first example - to get the desired result. The seventh snippet below, for instance, was used for the Labels list.

Identify the gadget to be displayed conditionally - and change the gadget identifier, as needed.

Having written the conditional code section as desired, use the Template Designer "Add CSS" wizard, and add the code. Remember to add blank lines above and below the code section being added.

  1. Homepage:
    <b:if cond='data:blog.url == data:blog.homepageUrl'>
  2. All Archive Pages:
    <b:if cond='data:blog.pageType == "archive"'>
  3. All Main Pages (Archive, Label, "Home" page and all "Newer / Older Posts"):
    <b:if cond='data:blog.pageType == "index"'>
  4. All Post Pages:
    <b:if cond='data:blog.pageType == "item"'>
  5. All Static Pages:
    <b:if cond='data:blog.pageType == "static_page"'>
  6. All Individual Posts:
    <b:if cond='data:blog.pageType == "item"'>
  7. Any Specified Page (Static Page, or Post URL - your choice):
    <b:if cond='data:blog.url == &quot;{URL}&quot;'>
    <b:if cond='data:blog.canonicalUrl == &quot;{URL}&quot;'>
  8. Any Other Than The Specified Page (Static Page, or Post URL - your choice):
    <b:if cond='data:blog.url != &quot;{URL}&quot;'>
    <b:if cond='data:blog.canonicalUrl != &quot;{URL}&quot;'>
  9. Any Other Than The Homepage:
    <b:if cond='data:blog.url != data:blog.homepageUrl'>

Note that "{URL}" refers to the URL of an actual page or post, such as
The value of "{URL}" must be provided with excruciating precision. It's best to display the page / post in question, then copy the URL from the address window, and paste it into your new conditional.

Note that the above shows the format of the opening "<b:if ... >". Each "<b:if ... >" must be properly paired with a carefully located closing "</b:if >". If you are unfamiliar with the concept of conditionals, and proper pairing of the opening and closing tags, please review my example, which shows how to display a Welcome post on the home page.

Note in cases #7 and #8, where {URL} is specified, you need to specify the canonical URL - not the country code alias, even if you are seeing the country code alias, for a blog published to BlogSpot.

If your blog is displayed as "" (if you are, for instance, in the UK), you need to use "". If you are specifying a custom domain, do not canonise the URL, specify the actual published URL (for instance "").

Be careful when entering the above statements. Single quotes ('), literal quotes (&quot;), and double quotes ("), are interchangeable in pairs, only in specific cases. Use them, in the appropriate circumstances - and correct sequence - religiously.
' ... &quot; ... &quot;'
' ... " ... "'
(single quote) ... (literal quote) ... (literal quote) (single quote)
(single quote) ... (double quote) ... (double quote) (single quote)
Open, then close, in reversed order.

Also, note the spaces, surrounding the conditional operator, in each statement. The spaces are not decorative, they are functional.
Always include the spaces.

Try one or two. Just don't forget to backup the template, before and after applying any tweak.

Dude, hit me with a comment!

.brittney. said...

Is it possible to use two of these conditional codes for one widget/blog element? What if, for example, I only wanted my labels widget to appear on two static pages and no where else. Is this possible? Thanks!

support@iHateMyBra said...

This is awesome.

Thank you for all you do!

Coach Sharon said...

I've been reading as many helps on this but am still confused. I tried some code changes as instructed without success. Either I missed something or need something more/different. Here is what I want to do:

Have the archive gadget applied to a "blog" page only, rather than the homepage. The homepage is just a welcome page basically that will occasionally change. No blogging done on homepage. The archive links can be seen on each page but only posts from the "blog" page would be listed. How do I do that?

Here's the eblogger website,

Chuck said...


You really need to discuss this in Blogger Help Forum: How Do I?, and we'll work on this together.

John said...

Thank you very kindly! I followed your instructions, and it worked perfectly. The precise and careful explanation is appreciated.

Omne M said...

Hi, is there a same method for removing the elements from static pages? I like to remove all social links from post footers as well as labels and header date.

Chuck Croll said...


Any page in your blog - whether dynamic (" ... /2012/07/ ..." or " ... /search/label/ ... ") or static (" ... /p/ ...") - can be included in a conditional, as long as you can provide the URL.