<mt:SubCategories>

A specialized version of the mt:Categories block tag that respects the hierarchical structure of categories.

<mt:SubCategories top="1">
    <!-- do something -->
</mt:SubCategories>

Attributes

Use of either top or category attribute is required.

Bug: As of MT4.261: mt:SubCategories outputs nothing by default Case 100897

category

Specifies a specific category by name for which to return categories.

<mt:SubCategories category="Italian">
    <!-- do something -->
</mt:SubCategories>

If two categories have the same label (“Italian” in this case), they can be specified by listing their parent category label followed by the cateory name, separated by a slash:

<mt:SubCategories category="Restaurants/Italian">
    <!-- do something -->
</mt:SubCategories>
<mt:SubCategories category="Recipes/Italian">
    <!-- do something -->
</mt:SubCategories>

If category label contains a slash (such as “Indian/Pakistani”) surround the value with square brackets:

<mt:SubCategories category="Restaurants/[Indian/Pakistani]">
    <!-- do something -->
</mt:SubCategories>

Either top or category is required.

include_current

A boolean attribute controling inclusion of the current category specified in the category attribute in the list.

Bug: As of MT4.261: mt:SubCategories attribute include_current doesn’t “include” current, rather it limits. Case 100896

sort_method

An advanced usage attribute. A fully qualified Perl method name to be used to sort the categories.

<mt:SubCategories sort_method="Your::Perl::Package::sort_by_entry_count">
    <!-- do something -->
</mt:SubCategories>

sort_order

Specifies the sort order of the category labels. Values “ascend” (default) and “descend”. This attribute is ignored if sort_method has been set.

Display a reversed list of top-level categories:

<mt:SubCategories top="1" sort_order="descend">
    <!-- do something -->
</mt:SubCategories>

top

If set to 1, displays only top level categories. Same as using TopLevelCategories.

List of top-level categories:

<mt:SubCategories top="1">
    <!-- do something -->
</mt:SubCategories>

Examples

Subcategories

List the subcategories of “Category AAA”

<ul>
<mt:SubCategories category="Cat AAA">
    <li><a href="<$MTCategoryArchiveLink$>"><$MTCategoryLabel$></a></li>
</mt:SubCategories>
</ul>

Recursive Category List

Recursively list all categories starting with top-level categories, only linking categories that contain entries (using mt:CategoryCount). (Using mt:SubCatIsFirst and mt:SubCatIsLast to condition for the first and last loops respectively):

<mt:SubCategories top="1">
    <mt:SubCatIsFirst>
        <ul>
    </mt:SubCatIsFirst>
    <mt:If tag="CategoryCount">
            <li>
                <a href="<$mt:CategoryArchiveLink$>" title="<$mt:CategoryDescription$>"><mt:CategoryLabel></a>
    <mt:Else>
            <li>
                <$mt:CategoryLabel$>
    </mt:If>
    <$mt:SubCatsRecurse$>
    </li>
    <mt:SubCatIsLast>
        </ul>
    </mt:SubCatIsLast>
</mt:SubCategories>

Display Parent, Sibling and Child Categories

Modify the following for your specific use case.

Use the following mtml on a Category archive to list related categories. List parent and child categories of the current category. If current category has a parent category, then return the parent category and any sub categories. If the current category does not have a parent category then just display subcategories…

<$mt:CategoryLabel setvar="CategoryLabel"$>
<mt:HasParentCategory>
    <mt:ParentCategory>
        <$mt:CategoryLabel setvar="ParentCategoryLabel"$>
        <li>Parent: <a href="<$mt:ArchiveLink$>"><$mt:CategoryLabel$></a></li>
        <mt:SubCategories category="$ParentCategoryLabel">
            <mt:If tag="CategoryLabel" ne="$ParentCategoryLabel">
                <li>Sibling: <a href="<$mt:CategoryArchiveLink$>"><$mt:CategoryLabel$></a></li>
            </mt:If>
        </mt:SubCategories>
        <li>Current: <$mt:Var name="CategoryLabel"$></li>
        <mt:SubCategories category="$CategoryLabel">
            <mt:If tag="CategoryLabel" ne="$CategoryLabel">
                <li>Child: <a href="<$mt:CategoryArchiveLink$>"><$mt:CategoryLabel$></a></li>
            </mt:If>
        </mt:SubCategories>
    </mt:ParentCategory>
<mt:Else>
    <mt:SubCategories top="1">
        <li>Sibling: <a href="<$mt:CategoryArchiveLink$>"><$mt:CategoryLabel$></a></li>
    </mt:SubCategories>
        <li>Current: <$mt:Var name="CategoryLabel"$></li>
    <mt:SubCategories category="$CategoryLabel">
        <li>Child: <a href="<$mt:CategoryArchiveLink$>"><$mt:CategoryLabel$></a></li>
    </mt:SubCategories>
</mt:HasParentCategory>
This page was last updated on 2009-10-29, 11:44. [Edit]

5 Notes

There's a bug in the way categories sort, although I can't find the bug report page (I can find the bug report page for 3.3, but not for 4.)

In the MT Admin Interface categories sort in a case insensitive way. When output to template, the categories sort in a case sensitive way.

This means, that if this is what I see in my interace: Animal Bumble Bee cat Dog emu Frog

when I output a list of categories (in my case using ) I get:

Animal Bumble Bee Dog Frog cat emu

Unfortunately, mt:SubCategories doesn't support the show_empty attribute that mt:Categories does, where you can choose whether or not to show categories that don't have any entries in them.

But! There's a solution using MT's conditional If tags. If the category contains entries, show the Category Label:

<mt:SubCategories>
    <mt:If tag="CategoryCount" ne="0">
        <$mt:CategoryLabel$>
    </mt:If>
</mt:SubCategories>

In an instance where using a For loop works with the content wrapped in a Categories tag, it doesn't seem to work when wrapped in a SubCategories tag. Is this maybe a bug?

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.