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. 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 constructs shown below can be substituted for the Welcome post - which is the one used in that article. The seventh construct below, for instance, was used for the Labels list.

  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;'>
  8. Any Other Than The Specified Page (Static Page, or Post URL - your choice):
    <b:if cond='data:blog.url != &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
http://blogging.nitecruzr.net/2010/10/conditionally-displaying-template.html
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 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 "myblog.blogspot.co.uk" (if you are, for instance, in the UK), you need to use "myblog.blogspot.com". If you are specifying a custom domain, do not canonise the URL, specify the actual published URL (for instance "www.mydomain.co.uk").

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;'
' ... " ... "'
or
(single quote) ... (literal quote) ... (literal quote) (single quote)
(single quote) ... (double quote) ... (double quote) (single quote)
Open, then close, in reversed order.

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

>> Top

7 comments:

.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, www.roseofbellarose.com.

Chuck said...

Sharon,

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

Omne,

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.

http://blogging.nitecruzr.net/2012/04/blogger-limits-pages-dynamic-and-static.html