Not a developer? Go to MovableType.com

Documentation

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.

While this tag can be used as a way to filter Entries, a much better approach is to use the category attribute of the Entries tag.

Attributes

  • label or name: 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.
  • 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:Entries>
    <mt:IfCategory>
        <!-- Entry is assigned a category -->
    <mt:Else>
        <!-- Entry is not assigned a category -->
    </mt:IfCategory>
</mt:Entries>

Determine if the Entry in context is assigned the category “News” and apply some special formatting; Entries in other categories are simply listed:

<ul>
<mt:Entries>
    <mt:IfCategory label="News">
    <li>News and Events:
        <strong><mt:EntryTitle></strong></li>
    <mt:Else>
    <li><mt:EntryTitle></li>
    </mt:IfCategory>
</mt:Entries>
</ul>

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

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

If you have need to test for many categories it may make more sense to use the If tag with the like attribute instead of multiple uses of this tag. Example:

<mt:Entries>
    <mt:If tag="EntryCategory"
            like="/(News|Events|Reviews)/">
        <!-- Display the News, Events, 
             and Reviews Entries -->
    <mt:Else>
        <!-- Display all other Entries -->
    </mt:If>
</mt:Entries>
Back

3 Comments

julian merrow-smith

julian merrow-smith on October 10, 2012, 3:52 a.m. Reply

Seems not to work with a comma seperated list of labels, ie. mt:IfCategory label=”foo,bar” However you can nest ifcategory statements seperated by mt:Else

ie (I’ll leave off the <> brackets)

mt:ifcategory label =”foo” do this mtelse mt:ifcategory labe=”bar” do the same as before mt:else do something different /mt:ifcategory /mt:ifcategory

I’ve had to do this for four categories in which I want the same thing to happen and not in the other 6 — seems kind of clumsy

julian merrow-smith

julian merrow-smith on October 10, 2012, 3:59 a.m. Reply

eg.

<mt:ifcategory label=”foo”><$MTInclude module=”the same module”$> <mt:else> <$MTInclude mo<mt:ifcategory label=”bar”><$MTInclude module=”the same module”$> <mt:else&gt<mt:ifcategory label=”foo2”><$MTInclude module=”the same module”$> <mt:else&gt<mt:ifcategory label=”bar2”><$MTInclude module=”the same module”$> <mt:else&gt<$MTInclude module=”a different module”$> </mt:ifcategory> </mt:ifcategory> </mt:ifcategory> </mt:ifcategory>

julian merrow-smith

julian merrow-smith on October 10, 2012, 4:05 a.m. Reply

Properly formatted:

<mt:ifcategory label=”foo”><$MTInclude module=”some module”$>
<mt:else>
<mt:ifcategory label=”bar”><$MTInclude module=”the same module”$>
<mt:else>
<mt:ifcategory label=”foo2”><$MTInclude module=”the same module”$>
<mt:else>
<mt:ifcategory label=”bar2”><$MTInclude module=”the same module”$>
<mt:else>
<$MTInclude module=”a different module”$>
</mt:ifcategory>
</mt:ifcategory>
</mt:ifcategory>
</mt:ifcategory>