Display categories that contain articles

A simple way of displaying categories that contain at least one article, using our new friend from Textpattern 4.07+ – if_variable.

The code

<txp:category_list wraptag="ul" break="">
	<txp:variable name="hasarticle" value='<txp:article_custom category=''<txp:category />'' limit="1" />' />
	<txp:if_variable name="hasarticle" value="">
	<txp:else />
		<li><txp:category title="1" link="1" class='<txp:if_category name=''<txp:category />''>current</txp:if_category>' /></li>
	</txp:if_variable>
</txp:category_list>

Note that this code will not output the number of articles contained in the category, but it will output a list of categories that have at least one article assigned.

Added for good measure is a class for the current category: class='<txp:if_category name=''<txp:category />''>current</txp:if_category>'.

This allows you to style your a tag (the default, unless you specify a wraptag attribute in the txp:category call) with something like:

a.current {background: #eee; text-decoration:none}
comments powered by Disqus