Rabu, 18 Agustus 2010

How To Tell The Page Type In Blogger Template

Q: In Google Blogger's template HTML I'd like to be able to tell what page type the current page is so I can treat it accordingly. How?

First of all here's a screen shot of the Blogger template HTML editor user interface:

Blogger Template HTML Editor Window

The text area is where you edit the template HTML so that your blog displays what you want it to display. All pages on your blog share this template, so if you want to treat pages differently according to their page type you need to create such conditional statements. For example you may want the homepage to contain a pitch line that you don't want other pages to contain; you may want label pages to simply list links to the corresponding posts without showing the actual post bodies; etc.

Here's the deal: How do you know whether the current page is the homepage, a single post, a static page, an archive page, or a label page? What condition do I need to test? You need to be able to make this distinction to treat each type of page differently. Surprisingly the Blogger documents and references regarding widget tags for layouts and page elements tags for layouts do NOT address this question thoroughly. They merely say

data:blog.pageType specifies the type of the current page. One of 'item', 'archive', or 'index'.

And it says nothing about what each value means. How frustrating Some values are obvious but others aren't. After some digging I figured out the following:

Value
index


item

static_page
archive
Meaning
homepage such as http://www.mensfashionforless.com/ or
label page such as http://www.mensfashionforless.com/search/label/Express
item page such as http://www.mensfashionforless.com/2010/01/zara-black-leather-jacket.html
static page such as http://www.mensfashionforless.com/p/about.html
archive page such as http://www.mensfashionforless.com/2010_07_01_archive.html


To tell homepage and a label page apart test whether data:blog.url equals data:blog.homepageUrl. If so then this is the homepage. Otherwise use data:blog.pageType. Here's the complete code that has a condition block for each page type:

<b:if cond='data:blog.url == data:blog.homepageUrl'>
<!-- homepage -->
<b:else/>
  <b:if cond='data:blog.pageType == &quot;index&quot;'>
  <!-- label page -->
  <b:else/>
    <b:if cond='data:blog.pageType == &quot;item&quot;'>
    <!-- single blog post -->
    <b:else/>
      <b:if cond='data:blog.pageType == &quot;static_page&quot;'>
      <!-- static page -->
      <b:else/>
        <b:if cond='data:blog.pageType == &quot;archive&quot;'>
          <!-- archive page -->
          <b:else/>
            <!-- unknown page; shouldn't be possible -->
          </b:if>
      </b:if>
    </b:if>
  </b:if>
</b:if>


Now you should be able to distinguish among the page types of your blog. Questions? Let me know!

0 komentar:

 
support by: infomediaku.com