Wednesday, September 13, 2006

Working around the "1 comments" bug

It is not really important, but nevertheless very annoying: if you have 1 comment on a post, Blogger Beta will say you have "1 comments" on your post.

Clearly a bug in the inner clockworx of Blogger, as we will see shortly. And a work-around is easy to implement.

How it works in Beta 

Open your template, switch to HTML-editing mode, and look for the code below:

<b:if cond='data:post.allowComments'>
<a class='comment-link' expr:href='data:post.addCommentUrl' expr:onclick='data:post.addCommentOnclick'>
<b:if cond='data:post.numComments == 1'>1 <data:top.commentLabel/>
<b:else/>
<data:post.numComments/> <data:top.commentLabelPlural/>
</b:if>
</a>
</b:if>


Now what is the meaning of all this haberdash?
In plain language it says:
If your post allows comments, then do the following:
If the number of comments is exactly 1, print "1 comment", and if it is not exactly 1, print "X comments".

The number of comments is stored in the Blogger database in the <data:post.numComments/>-field. The text that has to be printed is stored in single ("comment") and in plural ("comments") in the fields <data:top.commentLabel/> and <data:top.commentLabelPlural/>-fields respectively.
This is done so that you can customize this text to any other text you like, such as "silly input" or to another language.

I think this is where Beta shows a bug: clearly both the single and the plural version of the "comment" text are the same.
So what you can do is this:


  • Wait for Blogger to solve this bug, or

  • Implement my hack now!


How to work around the bug.

Below you can see the code for the workaround. Replace the <data:top.commentLabel/> with "Comment".

<b:if cond='data:post.allowComments'>
<a class='comment-link' expr:href='data:post.addCommentUrl' expr:onclick='data:post.addCommentOnclick'>
<b:if cond='data:post.numComments == 1'>1 comment
<b:else/>
<data:post.numComments/> <data:top.commentLabelPlural/>
</b:if>
</a>
</b:if>