mai 13, 2022

Blogger conditional tags and how to use it: Blogger unlike other popular CMS out there uses XML templates written in HTML, CSS, and JavaScript. A CMS Like WordPress used by many websites is coded using PHP.

That said, we won’t be discussing anything related to PHP coding language here, but rather we will be focused on Blogger conditional tags and how to use them.

Beside, the normal HTML tags used in Blogger templates, we also have Blogger’s own custom tags like the tag for creating sections(<b:section> …. </b:section>). The conditional tags are one of the custom tags used in Blogger XML templates and below we will be telling you more about them.

Blogger conditional tags and usage.

These special tags allow you to set actions for specific page types and widgets. You can use them to better the experience of your blog by limiting the display of certain template codes on specific device widths. So, you can use them to target CSS, meta tags and javascript.
Below we have a list of all the different Blogger conditional tags. Each of them has the structure

<b:if cond='condition_expression'>
    <!-- content when condition is satisfied -->

Conditional Blogger tags

Targeting 404 Error page

<b:if cond='data:blog.pageType == "error_page"'>
<!-- all error codes for 404 error pages here-->

Conditional tag targeting Homepage

<b:if cond='data:blog.url == data:blog.homepageUrl'> 
<!-- HTM, JS, CSS here will target only homepage -->

For Archive pages

<b:if cond='data:blog.pageType == "archive"'>
<!-- Enter codes to be  target all archive Pages here-->

For label pages

<b:if cond='data:blog.searchLabel'>
<!--  This will target all label pages -->

Conditional tag for static pages

<b:if cond='data:blog.pageType == "static_page"'>
<!-- Codes here will affect all static pages -->

Conditional tags for items(post) pages

<b:if cond='data:blog.pageType == "item"'>
<!-- all item pages -->

Search page

<b:if cond='data:blog.searchQuery'>
<!-- all search pages -->

For specific static pages

<b:if cond='data:blog.searchQuery == "foo"'>
<!-- for query 'foo' -->

Conditional tag targeting specific label

<b:if cond='data:blog.searchLabel == "foo"'>
<!-- for label 'foo' -->

For Index pages

<b:if cond='data:blog.pageType == "index"'>
<!-- all index pages -->

Tag for a particular post page 

<b:if cond='data:blog.url == data:blog.canonicalHomepageUrl + "p/foo.html"'> 
<!-- a specific static page with name 'foo' -->

Other conditional tags

Apart from the conditional tags above which can be used to;

  • Display Javascript content on particular occasions as well as hide them
  • Apply customised CSS to a particular post, page, archive page, static page or search page etc
  • Display widgets to specific blog sections or pages, etc

We also have situations where Blogger conditional tags. Some can be used to add run a script, display custom CSS and much more on different blog parts or display them selectively depending on certain factors.
For example, we are using the Blogger conditional tag with the else statement to display different description to label pages on our Blogger blog. In this case, we will make use of the <b:else /> tag and come out with an « OR » conditional tag.

<b:if cond='data:blog.searchLabel == "Blogger Widgets"'>
<!-- Add description box to page with label "Blogger Widgets -->
<b:if cond='data:blog.searchLabel == "SEO"'>
 <!-- Add description box to page with label "SEO"-->

 Looking back at the conditional tags we have above, the « == » sign there signifies equals to. On the other hand, we can use a different relational statement to reverse a condition. Here, we use « != « relational sign or operator.
That is;

  • ==  is for when the statement is equal to condition to make the comparison 
  • !=  is for when the statement is not equal to condition to make the comparison. 

<b:if cond=' data:blog.url == data:blog.homepageUrl'>
<!-- code will display if you are on homepage -->
 // code goes here...

<b:if cond=' data:blog.url != data:blog.homepageUrl'>
<!-- code here will not be displayed if you are on homepage -->
 // code goes here...

Another way of using the Blogger conditional tag is without adding the relational operators == or !=. In such a case, you use the conditional tag as follows.

<b:if cond='!data:post.allowComments'>
<! -- if comments not allowsed -->
// Code here will run

A condition can be combined by simply adding logical operator and/&& and or/|| to a conditional tag. When using the expression and/&& operator the conditional tag must return both true, otherwise, it will return false. While the logical operator or/|| operator, either one of the conditional tag must return true to satisfy the conditions or execute a particular code

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.