Skip to main content

Blogger Conditional Tag — b:if and b:else

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


List of Conditions


Techniques list


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 your post(s) list we wrote in January (01 ) of 2025.

Also, 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, like /2022/01/post.html. Put absolute URL like the example.

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

We can search them on the XHTML.

But, if we want to modify the custom error page, use that conditional statement to make the additional element(s) or CSS or JS appear or run only for that specified 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:

These are some of them:

  • Or

And others.


Escaping Quotation Mark

Remember to escape the " (double quotes mark) with ".

Example for the item Condition

From this:

Into 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/or JavaScript.

Please visit this post for more information regarding CDATA sections.


Comparing Operators

The double equals == means TRUE for non-numeric statement, and equal to for an integer (numeric).

Numeral

The == operator can be replaced by:

  • != other than that integer.
  • < less than.
  • > greater than.
  • <= less than or equal to.
  • >= greater than or equal to.

The [number_here] (number of comments) must be replaced by a non-negative integer (0, 1, 2,... , 200, etc).

Escaping

We need to escape the less than (<) and the greater than (>) marks.

  • less than (<) is &lt;
  • greater than (>) is &gt;

Example for escaping greater than operator

From this:

Into this:


Non-Numeral

We have two options:

  • == TRUE
  • != FALSE

Example of using negation (!=) for homepage statement:

It means do not show a specific content on homepage URL, but show the specific content on page which isn't homepage URL.


b:else

It's placed within the b:if

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.


b:elseif

It's also placed within the b:if

It means show content 1 on page which is homepage URL. And show content 2 through 4 with our specified criteria.


Stacking b:if

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 actually redundant. We can just use the [specific URL] condition for that.

For (latest) AND operation please read this post.

For (latest) OR operation please read this post.


b:if in Widget

To actually use the b:if and to control the widget appearance, we need to:

  1. Open the Blogger HTML Template.
    • Go to Template ➡️ Edit HTML
  2. 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 Template (using either way above), then jump (there's the widget button on top of the HTML template) to HTML1, expand it, and we will see these lines:

To control the whole HTML1 widget appearance, we can put the b:if right after the <b:includable>, and close the tag, using </b:if>, put it right before <b:include name='quickedit'/> — so that we can still see the edit icon by default (not being affected by the b:if).

Example:

That example means, the widget HTML1 will only be shown on item page. That is the post/article page itself, and not on any other, like homepage or blogroll or static page or other.


Tinkering Widget Content

We can put the custom content of the widget HTML1 via Blogger layout - or change the <data:content/> with our custom content. 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 JavaScript

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.

We can place it (the CSS/script) in the body or head section of the Blogger HTML.

Rule of thumb for placement:

  1. CSS is always placed on top of the elements being styled. Placing on top of body or on the bottom of head is the common practice.
  2. The script usually is placed at the bottom of the body. But then again, it depends on how the script is constructed.

Example for style (CSS):

Example for script (JS):


Mobile Template

This method, by default, only works for desktop template. If we need to make it work for desktop and mobile:

  • Go to Blogger TEMPLATE (theme) setting.
  • On the Mobile setting, we can either choose:
    1. Yes. Show mobile template on mobile devices.
    2. No. Show desktop template on mobile devices.
  • If we choose the first one, then pick the Custom option. Afterward, we need to work on the desktop view and mobile view separately.
  • If we choose the second option, then we only need to work on one template. This template will be shown on both desktop and mobile.

External References

Resources for more information about special Blogger tags:

Blogger Special Operators:

Additional:

MONKEYRAPTORBLOGGER
Last modified on

Comments

  1. Great explanation!

    I would like to use data:post.allowComments or data:post.showBacklinks because i'm making a menu that indicates which page we are reading. But i've got a error:
    "TEMPLATE ERROR: Invalid data reference post.allowComments: No dictionary named: 'post' in: ['blog', 'skin', 'view']".
    What i've to do to resolve this?

    ReplyDelete
    Replies
    1. Hi there Raquel, thanks for stopping by.
      Actually, to display the link (and the title) of the page you're reading, don't use those data tags.
      To display the TITLE of the post use : <data:post.title/>
      To display the URL of the post use : <data:post.url/>
      You need to put those in the body section of the blog (not in the widget).

      For instance, to create stuff like my sharer above, do this :

      <div style='text-align:center'>
        <p>Title : <data:post.title/></p>
        <p>URL : <data:post.url/></p>
      </div>

      I hope it will help. :)

      Delete
  2. Hello, I've been learning to use bif in my html..
    My question is, How can I make the social network share button count appear in the homepage and varies the count according to each post and not based on the homepage count..

    ReplyDelete
    Replies
    1. Hi there Nourel!
      For that, you need to separate the elements.
      If you use the share buttons I posted on this blog, then you need to add another different buttons for your homepage (home blogroll).
      The buttons example I shared here are the ones which show the different count for each post.

      Adding the homepage buttons :
      You don't need to go to HTML editor to add those.
      Just create a widget via layout ➝ then paste the elements and scripts from the original social networks in that element (without any b:if) ➝ save, and that's it.

      Anyway, you need to manually replace the URL of each button with the blog's homepage URL (not capturing the current page URL).
      You can find out how to do that on the original social network documentation and button generator page:
      1. Twitter : dev.twitter.com/docs/tweet-button
      2. Facebook : developers.facebook.com/docs/plugins/like-button
      3. Google+ : developers.google.com/+/web/+1button/

      If you want to make the homepage buttons only appear on homepage, then go to HTML editor ➝ jump to the widget you created for the buttons ➝ finally implement the b:if above.

      Hopefully that helps.

      Delete
  3. I wold like to use label name check ..how to write

    ReplyDelete
    Replies
    1. There's a Q&A about that on Stack Overflow. Here's the link: stackoverflow.com/questions/28060776/how-to-hide-blogger-posts-with-specific-label

      Delete
  4. Thanks for sharing the useful information! How if I need to combination labels with at least including one key label, such as:
    Label: ABC+EFG, ABC+XYZ
    That means any combination if label ABC is existed, will show up.

    I understand for single label, it is
    For combine 2 labels is

    So if I want ABC+EFG and/or ABC+XYG will show up, I don't use this because I have more than a dozen of combinations. So I think this way might work: with an asteric* but it's not working?

    Do you have any idea?
    Thanks!

    ReplyDelete
    Replies
    1. Hi,
      I never tinkered that kinda technique. So I have no clue. Sorry.

      Delete
  5. Great!

    I've added a label widget just below the main blog widget as opposed to the right or left menu.

    Is it possible to use this code to modify the label widget to have each post only display its own labels rather than displaying all the blog's labels. For example, if post 1 has labels a,b,c and post 2 has labels d,e,f then currently both posts with shows labels a,b,c,d,e,f. I'd to change this so that label widget only shows label a,b,c on post 1 and label d,e,f on post 2.

    ReplyDelete
    Replies
    1. I don't know if you will read this, Anonymous.

      The label widget has its own setting from the Blogger to display all the labels.
      If you wanna show some labels related to the post, then just show the bottom post labels list.
      Go to Settings ► Layout ► scroll down and find the "Blog Posts" ► click edit. You'll see there are blog post settings you can set.

      The other method is by requesting manually via Blogger API V2/V3 using JavaScript (XHR) to Blogger server for the current post. But of course, that will be super long to talk about right now. :)

      Delete
  6. Lots of good info, nice article!

    How would you suggest going about making a gadget appear exclusively on two specific URLs? In this case, a contact form that would appear on a services pages and the standalone contact page. I can't think of how to make it work. Two if cond URLs collide with eachother so that doesn't work. and I cant find anything that would work in the syntax like an 'or' statement, so it could read if 'this url' or 'this url'. Any ideas?

    ReplyDelete
    Replies
    1. The suggestion idea is on your second comment. :D

      Delete
  7. Bump. Forgot to check notify box to see ur reply haha..

    ReplyDelete
    Replies
    1. Hey Josh, thanks for visiting!

      Blogger has new syntaxes for "or" operator and "else if" and bunch others. So we now can have more than two conditions to compare. Here's the link: https://support.google.com/blogger/answer/46995?hl=en

      I'd suggest like so:
      <b:if cond='data:blog.url == "specific-URL-1"'>
          this is your gadget code / or JavaScript to control the gadget appearance
      <b:elseif cond='data:blog.url == "specific-URL-2"'/>
          this is your gadget code / or JavaScript to control the gadget appearance
      <b:elseif cond='data:blog.url == "specific-URL-3"'/>
          this is your gadget code / or JavaScript to control the gadget appearance
      <b:else/>
          something else...
      </b:if>

      You could use only b:if and one b:elseif and close it with b:else.

      Hope that helps. Cheers!

      Delete
    2. Yes sir! Works like a charm

      Delete
  8. Rich collection of Blogger Conditional Tag, if...else condition. To customize Blogger and its widget according your tips is awesome. Thanks for offering like article.

    ReplyDelete
    Replies
    1. Thanks for visiting, and you're very welcome!

      Delete
  9. Thanks for yur share :)...i'll copy that.

    ReplyDelete
  10. you have great article guys, may i use for references in my blog. thanks

    ReplyDelete
  11. Thanks for sharing this amazing post.

    ReplyDelete
  12. sangat bagus saya suka

    ReplyDelete

Post a Comment