Using IF ELSE in Blogger Blog

There is no doubt that WordPress is a better blogging platform than Blogger, but this is equally true that Blogger is an Ideal Start for newbies. It is a common trend that one starts with Blogger, Gains some maturity and then switches to WordPress. There are several problems with Blogger and most of the Blogger users are those who don’t have any Technical Background  For them, here is an explanation of using If Else type coding in the Blogger Template. This will enable bloggers to optimize their blog in different ways.

If Else type Conditional programing can be done by using <b:if cond=’ ‘>…..</b:if> tag. To Try this just log-in to your blogger account and move DashBoard > Layout > Edit HTML and check the “Expand Widget Templates” Option.

Now, take an objective: Lets say, we want to use different Meta tags for different pages (i.e. posts), which is not possible by any other method . Just enter the following  code at the proper place:

<b:if cond='data:blog.url == "http://yourblog.com/2008/06/yourpost.html"'>
<meta content='xyz,pqr' name='keywords'/>
<meta content='Your Description' name='description'/>
...............
</b:if>

Please Replace http://yourblog.com/2008/06/yourpost.html with the actual URL of that post. You will notice that the condition is made for the variable data:blog.url and equality operator == is used. You can use some other variables and operators. The != operator is used for Not Equal to cases .

I am sure that now you can guess the meaning of above source code. It simply says that if the URL of the page is equal to http://yourblog.com/2008/06/yourpost.html then apply the following meta tag. If you want to assign a meta tag set for the Home page of your blog then instead of using the URL of your homepage you can use data:blog.homepageUrl and your code will become like:

<b:if cond='data:blog.url == data:blog.homepageUrl'>
......
</b:if>

If you want to assign a meta tag set for all the post-pages (i.e., the pages other than your homepage) use this code:

<b:if cond='data:blog.pageType == "index"'>
.........
</b:if>

The variable data:blog.pageType looks for the type of page. Post pages are identified by item and homepage of your blog is identified by index. Hence, for homepage you can use both,  data:blog.url == data:blog.homepageUrl and data:blog.pageType == “index” .

You can use the above conditional coding in various ways to optimize your blog. I have several suggestions for you, but you can discover more ways as per your need.

  • Your home page will only feature the actual title of your blog, whereas post pages will feature your blog title, plus the title of your blog post. To Optimize your blog for Search Engines you need to modify it so that the past pages have titles of your blog post only. To get this done look for <title><data:blog.pageTitle/></title> and replace it with the following code
    <b:if cond='data:blog.pageType == "index"'>
    <title><data:blog.title/></title>
    <b:else/>
    <title><data:blog.pageName/></title>
    </b:if>
  • Similarly , sometimes you want to display some page-elements on homepage and not on post pages ( or , vice-versa ) . You can use this conditional coding again for this objective . Just Go to Page Element section and select the required page element to edit . The editor will open in a new window .Just focus on the URL of this window. At the end you can find the widgetId=HTML# . This is the widget ID of this page element . Now , switch back to the EDIT HTML section and locate that widget ( may use Find option of your browser ). You will notice a layout tag <data:content/> inside this widget code . This tag displays the contents of that page element on any page. You can do your conditional programing around this. For example you want to hide it from homepage, just do this:
    <b:if cond='data:blog.pageType == "item"'><data:content/></b:if>

    This trick can be used for many purposes, like displaying Ads, Featured Posts, Special Messages for visitors.

I hope that this article will help you in optimizing your Blogger blog. If you need any help, feel free to contact us. 🙂


[Editor Note: This post is by guest blogger Arpit Kumar. He blogs at TechRaga on Internet & technology. He is very active twitter user, in case if you like to follow him on twitter.

If you like to write for Devils Workshop, please check this. Details about our revenue sharing programs are here.]

20 Comments

Gaurav September 22, 2008

Ahh Nice post… for once I thought, Rahul is back to Blogger tuts 😛
good work mate 😀

Deepak September 22, 2008

When I was in blogger there were no one to help me out..:( And when I’ve migrated to wordpress arpitnext is born.. 😛

Vineet Tripathi September 23, 2008

Great post, proved very helpful for me as i was looking for exactly this.

Rahul Bansal September 25, 2008

@Gaurav
Well, wordpress have no exit my friend! 😉

@Deepak
I felt same… :O

@Vineet
Arpit is really kool with blogger templates so if you have some questions, you can post them here. 🙂

Abhishek September 28, 2008

IF else in XML !! aaah i hate XML

Rahul Bansal October 1, 2008

@Abhishek
XML is not programming language btw.
This IF-ELSE is parsed by Blogger at backend.
I hated Blogger for this as they forced to learn stuff like above.

At wordpress, everything is plain PHP! 🙂

Prateek February 23, 2009

Good Tutorial

Toure March 23, 2009

Thanks for the tips.
However, what about the case I want to use the || operator (or)? I haven’t try it yet because I really wonder where to put it. Should I rewrite the full if statement precede by || or simply put the second parameter inside the same if? Anyway, I am gonna try both today. If you have any suggestion, please let me know.
Again thanks.

Toure April 21, 2009

Hello.
Is there a way I can hide a post and show it only on that post url?
or hide it from showing on the homepage at any moment?
for exemple can I hide my last post for showing on my homepage?
thanks for any guidance.

Toure April 21, 2009

Never mind!
thanks
I fugure out by using the if condition of to show data:post.body when !=homepage

BUZZoianu' September 1, 2009

How can i put “or” syntax in if condition ? For example if ‘condition1’ or ‘condition2’ do smth1 else do smth2

Siva October 19, 2009

Can you please tell me the way to “stop showing ads for the author”. As I often visit my blog which has few pages, I’m forced to get useless impressions of adsense. I really require solution for this. I searched google a lot but, can’t find solution.

Sarah November 29, 2009

I have created menu tabs for pages (Memories, Recipes, Tips, etc.) and would like to post to those pages without the post showing on the Home page. (I have added coding to prohibit the post from posting to the Home page.)

If you can help – please provide coding to make the above happen, and show me where it needs to go in the HTML layout.

Thanks much!!

Akshay Kakkar January 24, 2010

What is the if else statements to be used for formatting the pages(new feature) on my blogspot blog?

Jim March 17, 2011

I was wondering if I’d be able to use an if/else statement to load only certain portions of my blog.

For instance, on my Blogger blog I have multiple static pages and when I go from one to another the entire page reloads including the header and sidebar even though they contain the exact same content on each page. Can I use if/else to load only the main content of a static page and not reload the header and sidebar when someone goes between pages. I mainly want to do this to speed up loading. Additionally I have a music player which stops and reloads every time the user goes to a different static page. Any help would be greatly appreciated. Thanks.

Ravish April 13, 2011

very great and useful tip. Thanks man

Mohamed Samir May 1, 2011

its awesome post, really useful i was looking for something like dat to use it in my blog, thx @ArpitNext

Zahid November 3, 2011

Hey, Nice post. Thanks for sharing. I guess it will work just as it works in wordpress blogs.