This article will explain how you can limit the appearance of a widget to a specific type of page on your blog.
For the purpose of explanation, I will use a widget from Feedjit, which we can use to guide visitors to popular pages on a blog. This particular widget will display the other pages read by previous visitors to a given page in your blog. It works similar to the Amazon’s “Customers Who Bought This Item Also Bought” recommendation. It is clear that this type of a widget is meaningless on your blog's homepage unless otherwise you have limited the number of posts on the homepage to one.
So let's see how we can limit this widget to only individual post pages or item pages. We use the conditional element or the <b:if>...</b:if> element for this purpose. The conditional element allows us to instruct Blogger to display some other element only if a given condition is true. Here's the general format of this element.
<!-- The element to be conditionally included goes here -->
</b:if>
The opening tag of the <b:if> element has an attribute called cond, meaning condition. The value that goes in to this attribute is a Boolean expression (don't worry, in simple English that means a check that turns out to be either true or false). If this expression evaluates to 'true', the element inside the <b:if> element will be included in the page rendered on a browser.
First step, obviously, is to add the widget as you would normally do. This widget will be installed using an HTML/Javascript element from the Layout editor. (Tip: Provide a Title for this widget. For example, I gave the title "Popular Pages" to the widget I used for this article. This is useful to locate the installed widget in our next step. You can remove the title later if you don't want it.)
Then we switch to the Edit HTML mode of the Layout editor and click on the Expand Widget Templates check box. (Always remember to back up your template, to restore if something goes wrong). Don't be overwhelmed by the expanded template; it's just like the unexpanded one but with more HTML. Now do a Find (Ctrl+F) for the Title we gave in the above step, inside the expanded template and locate the widget you just added.
This widget will be included as a <b:widget> element in the template. The widget has two parts; its title and the content. Locate the content part of the widget element and encompass that whole section inside a <b:if>...</b:if> element.
The important thing is the condition we provide here. We give the condition as cond='data:blog.pageType == "item"' instructing Blogger to display the content of this widget only if the pageType is equal to 'item'.
The final code snippet will look as follows.
<b:includable id='main'>
<!-- only display title if it's non-empty -->
<b:if cond='data:title != ""'>
<h2 class='title'><data:title/></h2>
</b:if>
<b:if cond='data:blog.pageType == "item"'>
<div class='widget-content'>
<data:content/>
</div>
</b:if>
<b:include name='quickedit'/>
</b:includable>
</b:widget>
See the figure below for an illustration of this step.
The blue rectangle in the above figure shows the content section of the widget. The two red rectangles show the <b:if> element that we place enclosing the content section.
The same method can be used with the pageType set to 'index', to limit a widget to your blog's homepage.











6 comments:
My public blog www.kelloggscamels.com has 6 authors. Is it possible to set up a condition that shows widgets for pages <> homepage AND with a certain date?
Thanks for your post!
John
Sorry, it seems not possible.
I tried to use post.dataHeader or post.timestamp to do what you need, but didn't succeed.
Thanks, but (/b:if>
(b:if cond='data:blog.url == "http://www.kelloggscamels.com/2008/09/pb.html"'>
(div class='widget-content'> did a sufficient job. Now only on some specific url's a picture will appear on the top of the blog below the menu bar. Of course I would have loved to used a wildcard so that for instance all (/b:if>
(b:if cond='data:blog.url == "http://www.kelloggscamels.com/2008/09/** URLs would applied to this condition. In stead and due to my limited knowhow (did not know about Blogger 3 weeks ago) I used the above statement for each URL that applied.
But it was your idea that triggered this :)
Thanks, John
Hi,
Is it possible for me to have a widget to show on all of my individual post pages and main pages only? Basically I have 6 pages where I do not want the widget to show.
Hi Sarah,
Your question is not very clear, but Blogger supports three values in the 'pageType' property, namely, 'item', 'index', and 'archive'.
'item' is for individual pages, 'index' means the home page and 'archive' refers to the pages you get when you click on Labels.
Hi ids,
This statement is for specific pages. But it is not clear it specific for which page or which label?
If I want to made widget display only under one label, how should I go for it?
Thanks!
Jack
Post a Comment
Thanks in advance for all your comments and suggestions. They are very much appreciated.
IMPORTANT:
If you have a specific technical question, please email me with a description of the problem and the blog's URL.