Go to main content

If category




The if_category tag is a conditional tag and always used as an opening and closing pair, like this…

    …conditional statement…

The tag will execute the contained statements if the name attribute matches a category search value, or the list is an article list by category.

Should be used in a page template; if checking the category in an article form, use if_article_category.


Tag will accept the following attributes (case-sensitive) as well as the global attributes :

category="category" v4.7.0+
Category name. Note the category name is specified in lower case regardless of how you typed its title in the Categories panel. Also note that if you had called your category ‘My Category Name’ it becomes ‘my-category-name’ when used in tags.
Default: the current listing category, if any.
Comma-separated list of category names.
Default: unset, which determines whether ‘‘any’’ category listing is being viewed.
parent="category or integer" v4.7.0+
Comma-separated list of category names or an integer. In the first case, the tag will check if category or some of its ancestors belong to the parent list. In the second case, the tag will check if category’s ancestor of this generation belongs to the name list.
Default: unset.
type="context" v4.3.0+
Textpattern context to check against. You can choose from the following contexts (set to empty to include all contexts):
article is this an article category list?
image is this an image category list?
file is this a file category list?
link is this a link category list?
Default: article.


Example 1: Display info depending on list category

<txp:if_category name="prose">
    <p><txp:author /></p>

Displays the author’s name if the article list is of category prose.

Other tags used: author.

Example 2: Use tag with else

<txp:if_category name="prose">
    <p><txp:category /></p>
<txp:else />
    <h3><txp:site_name /></h3>

Displays the category name if the article list is of category prose, otherwise show the site’s name.

Other tags used: category, else, site_name.

Example 3: Display an appropriate heading

   <h3>Articles in category <txp:category title="1" /></h3>
<txp:else />
   <h3>All articles</h3>

Displays an appropriate heading for both category and non-category pages.

Other tags used: category, else.

Example 4: Display a category/article list

Given the defined article categories: prose, poetry, and opinions.

<txp:category_list label="Category Navigation" wraptag="p" />

<txp:if_category name="prose">
    <txp:recent_articles limit="25" break="li" wraptag="ol" label="Prose" category="prose" />

<txp:if_category name="poetry">
    <txp:recent_articles limit="25" break="li" wraptag="ol" label="Poetry" category="poetry" />

<txp:if_category name="opinions">
    <txp:recent_articles limit="25" break="li" wraptag="ol" label="Opinions" category="opinions" />

Shows a category list and, underneath it, a list of related articles in the currently selected category. Changing the category using the list changes the related articles underneath.

Other tags used: category_list, recent_articles.

Example 5: Check if ‘cat’ is descendant of ‘animal’

<txp:if_category category="cat" parent="animal">
    <p>Cat is an animal.</p>

Example 6: Check if ‘anakin’ is the father of ‘luke’

<txp:if_category category="luke" parent="1" name="anakin">
    <p>The test is positive - I am your father!</p>


Version 4.7.0

category and parent attributes added.

Version 4.3.0

type attribute added.

If you notice any kind of problem with this page's construction or content (outdated information, typos, broken links, or whatever), open an issue to have it sorted. Or have a go at it yourself. :)