Blogger Conditional Tag — b:if and b:else
<b:if>
and <b:else/>
only work if you put them in the XHTML of the Blogger blog, not in the HTML/JS widget (via layout).List of
<b:if>
conditions #- Item
- Static
- Index
- Archive
- Homepage
- Specific URL
- Error Page
- Backlink
- Display Name
- Number of Comments
- Jump Link
- Commenting Enabled
- Others
Techniques list #
- Escaping the quotation
- Comparing operators
- <b:else />
- Stacking multiple
<b:if>
s - Using it for a widget
- Tinkering widget content
- Global
CSS/JS
- Mobile template?
- More references
Show content only on item page (every single post):
Show content only on static page(s):
Show content only on index page (sorted by label and date):
Show content only on archive page (your blogroll — sorted by date):
Show content only on homepage URL of the blog:
Show content only on a specific URL on the blog:
We need to put complete URL and protocol for that, such as:
http://mysupersweetblog.blogspot.com/2002/01/hello_hoho.html
Do not use relative URL, like just
/2002/01/hello_hoho.html
.Show content on the error page of the blog (
404
- not found page):There's the predefined elements and styling provided by Blogger for that.
They're the elements starting with class name
status-msg-
.You can search them on your
XHTML
.But, if you wanna modify the custom error page, use that conditional statement to make the additional element(s)/CSS/JS appear/run only for that criteria.
Show content only on post(s) which is/are showing backlink:
Show content only on post(s) which is/are showing specific display name (such as the post's author):
Show content only on post(s) which has/have specific number of comment(s):
Show content only on post(s) which has/have jump link (the "read more" link):
Show content only on post(s) which is/are comment enabled:
There are plenty other of conditions. But they're "deeper", sort of speak.
These are some of them:
<b:if cond='data:blog.metaDescription'>
<b:if cond='data:mobile'>
<b:if cond='data:title'>
or<b:if cond='data:title != ""'>
<b:if cond='data:showThumbnails == "false/true"'>
<b:if cond='data:showSnippets == "false/true"'>
<b:if cond='data:post.thumbnail'>
<b:if cond='data:display == "list"'>
<b:if cond='data:blog.url == data:label.url'>
<b:if cond='data:showFreqNumbers'>
<b:if cond='data:useImage'>
Escaping quotation mark #
Example for the
From thisitem
conditionInto this
The examples in here didn't escape the quotation marks within the
b:if
to make them easy to read.
Actually, the quotation marks (double or single) will automatically be replaced by Blogger, but, you know, just in case.
We can also use
CDATA
section for escaping characters but not for b:if
. It's for the characters within CSS
and/orJavaScript
. Read some more on Comparing operators #
There are conditions that using double equals (comparing method), and just direct statement. You can see on the list above.
The double equals
==
means TRUE for non-numeric statement, and equal to for an integer (numeric).
Numeral #
The==
operator can be replaced by:
The
!=
other than that integer.<
less than.>
greater than.<=
less than or equal to.>=
greater than or equal to.[number_here]
(as you can see on number of comments) must be replaced by a non-negative integer (0, 1, 2,... , 200, etc).
Example for escaping greater than operatorFrom this
Into this
Non-numeral #
We only have two options for the operator:
==
TRUE!=
FALSE
Example of using negation (!=
) forhomepage
statement:
It means do not show a specific content on homepage URL, but show the specific content on page which isn't homepage URL.
How about
<b:else />
? #Let's take a look at the conditional tag using double equals.
For example:
It means show content 1 on page which is homepage URL. If the page isn't homepage URL, then content 2 will be shown.
Different
<b:if>
conditions can be stacked #Example:
It means that content 1 will be shown on item page, but not on specific item page (URL). And when the page is an item page and it has the specific URL stated, it will then show the content 2.
That stacking example is very much redundant. We can just use the
[specific URL]
condition for that.Nevertheless...
Neat, isn't it?
Using
b:if
for a widget #To actually use the
<b:if>
and <b:else>
to control the widget appearance, we need to:- Open the Blogger HTML Template.
- Go to Template ► Edit HTML
- Or shortcut, using URL pattern (if you're logged in):
https://www.blogger.com/blogger.g?blogID=[your_BLOG_ID]#templatehtml
the [your_BLOG_ID] is your Blogger blog ID number.
- Then go (jump) to a specific widget.
ID
HTML1
, we'll need to open our Blogger HTML Template (using either way above), then jump (there's the widget button on top of the HTML template) to HTML1
, expand it, and you'll be seeing these lines:To control the whole
HTML1
widget appearance, you can put the <b:if>
right after the <b:includable>
, and close the tag, using </b:if>
, put it right before the closing </b:includable>
.Example:
That example means, the widget
HTML1
will only be shown on item page. That is thepost/article page itself, and not on any other, like homepage or blogroll or static page or other.Tinkering the content of a widget #
You can put the custom content of the widget
HTML1
via Blogger layout - or just change the <data:content/>
with your custom content - not highly recommended though. Do this if you're confident about tinkering Blogger template.Example:
Example with
<b:else />
It can be placed like so:
Global CSS and/or script #
The conditional
<b:if>
can be used as conditional styling (CSS) or script not only for a particular widget - it can also be used for the entire blog layout.You can place it (the CSS/script) in the
body
or head
section of your Blogger HTML.Rule of thumb for placement:
CSS
is always placed on top of the elements being styled. Placing on top ofbody
or on the bottom ofhead
is the common practice.- The
script
usually is placed at the bottom of thebody
. But then again, it depends on how you constructed your JavaScript.
Example for styling (CSS) - it's the same thing with the scripting (JavaScript/jQuery):
Mobile template #
This method, by default, only works for desktop template. If you need to make it work for desktop and mobile:
- Go to your Blogger TEMPLATE setting.
- On the Mobile setting, you can either choose:
- Yes. Show mobile template on mobile devices.
- No. Show desktop template on mobile devices.
- If you choose the first one, then pick the Custom option. Afterward, you need to work on the desktop view and mobile view. It's still using your (desktop) global template, but with slightly different Blogger data tags layout, sort of.
- If you choose the second option, then you'll only need to work on one template, the desktop. This template will be shown on desktop or mobile.
I only tinkered the desktop template and put some conditional JavaScript on certain posts/static pages on mobile view. Each condition is embedded on each page, it's scoped.
Not for the entire Blogger XHTML because it will be stripped.
Read some more about Blogger's mobile template on
External References #
Resources for more information about special Blogger tags:
- Layout data tags
- Widget tags for layouts
- Page element tags for layouts
The Blogger special operators:
- Documentation on
Additional:
- Finding out your Blogger ID number
Last modified on August 22, 2015
0 nhận xét: