<mt:IfCategory>

A conditional tag for determining if the entry in context is assigned categories. Determine if in a specific category and/or if the category is a primary or secondary category.

<mt:IfCategory>
    <!-- Entry is assigned a category -->
</mt:IfCategory>

Attributes

label

The name of a category. Tests the category in context (or categories of an entry in context) to see if it matches with the given category name.

name

Alias of label.

type

Either the keyword “primary” or “secondary”. Used to test whether the specified category (specified by the label attribute) is a primary or secondary category assignment for the entry in context.

Examples

Determine if the entry in context is assigned a category:

<mt:IfCategory>
    <!-- Entry is assigned a category -->
<mt:Else>
    <!-- Entry is not assigned a category -->
</mt:IfCategory>

Determine if the entry in context has a secondary category assigned:

<mt:IfCategory type="secondary">
    <!-- entry in context is assigned to one or more secondary categories -->
</mt:IfCategory>

Determine if the entry in context is assigned the category “News”:

<mt:IfCategory label="News">
    <!-- entry in context is assigned to the "News" category -->
</mt:IfCategory>

Determine if the entry in context is the in the primary category “News”:

<mt:IfCategory label="News" type="primary">
    <!-- entry in context is assigned the primary category "News" -->
</mt:IfCategory>
This page was last updated on 2009-06-23, 19:06. [Edit]

5 Notes

Hi there!

Is it possible to give the attributes a value with "MTGetVar" ?

Like this?

<MTIfCategory label="<$MTGetVar name="ThisBlog"$>">

Cheers kai

You can use this tag to display all entries EXCEPT those in a certain category, which is something that was really clumsy before and required plugins and scripts to make happen. For example:

<mt:Entries>
    <mt:IfCategory label="Special Events"> 
    <mt:else>
    <li><a href="<$mt:EntryPermalink$>"><$mt:EntryTitle$></a></li>
    </mt:IfCategory>
</mt:Entries>

...will return a list of all entries EXCEPT those in the Special Events category.

Conversely, you could use a different set of parameters and have the Special Events Category display differently than everything else. As a simple example:

<mt:Entries>
    <mt:IfCategory label="Special Events">
    <li><a href="<$mt:EntryPermalink$>"><strong><$mt:EntryTitle$></strong></a></li> 
    <mt:else>
    <li><a href="<$mt:EntryPermalink$>"><$mt:EntryTitle$></a></li>
    </mt:ifcategory>
</mt:Entries>

...will return a list of all entries and all the entries in the Special Event category will be in bold type.

@kai -- yes, this is possible. Here's an example:

<$mt:Var name="label_name" value="foo"$>
<mt:IfCategory label="$label_name">
    true if $label_name = foo
</mt:IfCategory>

The attributes show as optional, and the first example illustrates that you can use MTIfCategory all by itself, presumably to test if an entry has a category at all. But as of MT 4.21, if you use it as such, it throws an error in an Entry template, saying that you did not specify the "type" attribute, implying that this usage is not permitted.

Until a fix is implemented use <mt:If tag="EntryCategory"></mt:If>

Leave a note

Have a question? Please use the MT Forums. Notes submitted on documentation should pertain to tips & hints regarding documentation. Your note may be removed once its contents have been integrated into the body of the page.