Using txp:variable to improve your comment header

You can tailor your comment header to the cases when you have 0, 1, or multiple comments. First, store the comment count in a <txp:variable />:

<txp:variable name="num_comments" value='<txp:comments_count />' />

Then, use nested conditional tags to separate the three cases. TXP provides <txp:if_comments> natively, and then we also have <txp:if_variable> like so:

<h3 id="comments-header">
<txp:if_comments>
	<txp:if_variable name="num_comments" value="1">
		<!-- output when there is ONE comment -->
		Just 1 comment so far&#8230;<a href="#comment-form"><txp:comments_invite textonly="1" showalways="1" showcount="0" /></a>
	<txp:else />
		<!-- output when there is MORE THAN ONE comment -->
     		<txp:comments_count /> comments so far&#8230;<a href="#comment-form"><txp:comments_invite textonly="1" showalways="1" showcount="0" /></a>
	</txp:if_variable>
<txp:else />
	<!-- output when there are ZERO comments -->
	No comments yet&#8230;Be the first!
</txp:if_comments>
</h3>

For the cases when there are 1 comment or more than 1 comments, I’ve enclosed the <txp:comment_invite /> in a link to the comment form on the page. When there are no comments, I know the comment form will directly follow the comment header, so there is no need to provide a link.

You can see an example of this set up in action here: Behind the Knife.

General principal

In general, you can use <txp:variable /> and its conditional counterpart, <txp:if_variable> (and the brilliant tags-as-attributes feature) to create conditionals that Textpattern doesn’t provide natively.

comments powered by Disqus