Textpattern tips, tutorials and code snippets

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}

1 Comment Comment feed

Hi, Love the design of your site. One thing which draws me to textpattern is easy availability of lots of minimal templates – templates with lots of white spaces. I am more and more tempted to run a blog using textpattern. I guess from what I have read so far, textpattern is far more powerful and flexible than wordpress though wordpress has a better backend user interface.

Add a comment

Use Textile help to style your comments