Greetings!
Blogger has special b:if, b:elseif, and b:else tags.
Those only work if we put them in the XHTML of the Blogger blog, not in the HTML widget (via layout).
b:if = beef & b:else = bells.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):
For instance, this URL pattern:
It will show your post(s) list with a particular [YOUR_LABEL] label.
- Change the
[your_blog_url]with your own blog host URL. - Change the
[YOUR_LABEL]with your own label.
Show content only on archive page (your blogroll — sorted by date):
For instance, this URL pattern:
It will show the post(s) list we wrote in January (01) of 2025.
Change the [your_blog_url] with your actual blog URL.
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:
⚠️ Do not use relative URL such as /2022/01/post.html. ❌
ðĄ Put absolute URL as shown in the example. ✅
Show content on the error page — the 404 or "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-. We can search them in the XHTML.
By default, we can customise the error page through Settings ➡️ Errors and redirects ➡️ Custom 404.
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 author of the post):
Show content only on post(s) which has / have specific number of comment(s):
The [number_here] must be replaced by a non-negative integer (0, 1, 2,... , 200, etc.)
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:
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'>
And others.
Remember to escape the:
"(double quotes) with"- and
'(single quotes) with'.
Example of escaping double quotes.
From this:
To this:
The examples in here do not escape the quotation marks within the b:if to make them easy to read.
Actually, the quotation marks (single or double) will automatically be replaced by Blogger — but, you know, just in case, for our own additional knowledge.
We can also escape reserved characters using a CDATA section, but this only works inside style and script tags, not inside b:if. Please visit this post for more information regarding the CDATA sections.
The double equal == means matches exactly for non-numeric values (such as strings), and is equal to for numbers.
The comparison operators:
==equal to.!=not equal to.<less than.>greater than.<=less than or equal to.>=greater than or equal to.
We need to escape the less-than (<), greater-than (>), and ampersand (&) characters — also the single and double quotes as I've mentioned above.
- Less than (<) is
< - Greater than (>) is
> - Ampersand (&) is
&⬅️ This is one example of error thrown by Blogger's XML parser if we don't escape it.
Let's see an example of escaping the greater-than (>) symbols.
From this:
To this:
We have two options:
==means matches exactly.!=means does not match.
Example:
It means if the current URL does not match the homepage URL, show that specific content.
In other words, the specific content is displayed on any URL other than the homepage URL.
It is placed within the b:if tag.
Example:
It means show content 1 if current URL matches the homepage URL. If the current URL doesn't match homepage URL, show content 2.
b:elseif is also placed inside b:if, but before b:else.
It means show content 1 on page which is homepage URL. And show content 2 through 4 with our specified criteria.
We can also define only the b:elseif without needing to end the entire condition with a b:else.
It means show content 1 on page which is homepage URL. And show content 2 through 3 with our specified criteria. Other than the specified criteria, nothing will be executed.
It means that content 1 will appear on all individual post (item) pages, except for the specific URL defined in the condition. If a visitor is on that particular excluded post — or on any other page that isn't a post — the template will display content 2 instead.
The example above is actually redundant. We can simply use the [specific URL] condition for that. Like so:
⬆️ Simpler.
Related posts for AND and OR operators:
To actually use the b:if and b:else to control the widget appearance, we need to:
- Open the Blogger HTML editor.
- Navigate to Theme. ➡️ Open drop down on the Customize button. ➡️ Go to Edit HTML.
- Then go (jump) to a specific widget.
For instance, if we want to tinker the appearance of HTML1 widget, we'll need to open our Blogger HTML text editor (using either way above), then jump — we can find Jump to widget button on top of the text editor — to HTML1, expand it, and we will see these lines:
To control the entire HTML1 widget appearance, we can —
-
put the
b:ifright after the<b:includable>, close it with a</b:if>, put the entire set right before<b:include name='quickedit'/>— so that we can still see the edit icon of that widget.Example:
That example means the widget
HTML1will only be shown on item page — that is the blog post (article) page and not on any other like homepage, blogroll, static page, or other. Or, we can use the simpler technique.
We can place our custom content in the widget HTML1:
- via Blogger layout,
- or disable (comment out) the
<data:content/>tag and place our custom content after or before that.
Example of disabling <data:content/> tag:
Example with b:else
It can be placed like so:
Global CSS and / or JavaScript
We can use b:if for conditional styles (CSS) or scripts (JS), whether for a single widget or the entire blog layout.
We can place our custom CSS and / or JS in the body or head section.
Rule of thumb for placement:
-
The
styletag is always placed above the elements being styled. Placing it within theheadsection is the standard practice. -
The
scripttag usually is placed at the bottom of thebody. But then again, it depends on what the script is for. It can also be placed in theheadsection.
Example of CSS:
Example of JS:
This method, by default, only works for desktop template. If we need to make it work for desktop and mobile, then —
- go to Blogger TEMPLATE (theme) setting.
- On the Mobile setting, we can choose —
- Yes ➡️ Show mobile template on mobile devices.
- No ➡️ Show desktop template on mobile devices.
- If we choose the Yes, then pick the Custom option. Afterwards, we need to work on the desktop view and mobile view separately.
- If we choose No, then we only need to work on one template. This template will be shown on both desktop and mobile.
