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

No related posts.

18 Comments…

 Share your views
  1. Ahh Nice post… for once I thought, Rahul is back to Blogger tuts :P
    good work mate :D

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

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

  4. @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. :-)

  5. IF else in XML !! aaah i hate XML

  6. [...] Template is now my favorite topic. I have already written a post which helps in understanding the use of if-else conditional tags. We can use the same logic for [...]

  7. [...] have written an article on popular blog Devil’s Workshop . I hope that  this article will be quite helpful for you [...]

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

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

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

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

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

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

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

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

  16. very great and useful tip. Thanks man

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

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

Leave a Comment

  • Facebook
  • GitHub
  • Twitter
  • LinkedIn
  • Google

Your email address will not be published.

*