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.
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.
sort_by
Specifies the sort key. The following options are available. This attribute was introduced in Movable Type 5.1.
- label
- description
- baseame
- created_on
- modified_on
- user_custom
The default value is user_custom. It produces the sort order you specified on the Manage Categories screen. This attribute is ignored if sort_method has been set.
Display a reversed list of top-level categories sorted by the label:
<mt:SubCategories top="1" sort_by="label" sort_order="descend">
<!-- do something -->
</mt:SubCategories>
sort_order
Specifies the sort order. Values “ascend” (default) and “descend”. This attribute is ignored if sort_method has been set.
sort_method
An advanced usage attribute. A fully qualified Perl method name to be used to sort the categories. This attribute is not supported in the dynamic publishing.
<mt:SubCategories sort_method="Your::Perl::Package::sort_by_entry_count">
<!-- 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>
Exclude Empty Categories
The SubCategories tag doesn’t support the show_empty
attribute that the Categories
tag does, where you can choose whether or not to show categories that don’t have any entries assigned to them.
But! There’s a simple 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>