Article
Using subcategories in MovableType 3
I promise this will be the last post about subcategories in MT3. I just want to document this technique (free of all the drama) for myself and others.
One of the major features that appeared in MovableType 3 is subcategories. This functionality is based on David Raynes’ excellent Subcategories plugin, but unfortunately it is poorly documented.
Perhaps the most poorly documented tag is the essential MTSubCategories. This is a replacement for MTCategories and, near as I can tell, the foundation of any template that makes use of subcategories. Here’s what the MovableType documentation has to say about this tag:
MTSubCategories
MTSubCategories is a replacement for MTCategories that will respect the hierarchical structure of your categories
And that’s it.
It should be noted, however, that you can nest MTSubCategories inside of itself. Doing so will step through each subcategory of a given category and do whatever it finds inside of that second-level MTSubCategories tag.
For a quick example, let’s say your template code looks like this:
1 <MTSubCategories>
2 <h4><$MTCategoryLabel$></h4>
3 <ul>
4 <MTSubCategories>
5 <li><$MTCategoryLabel$></li>
6 </MTSubCategories>
7 </ul>
8 </MTSubCategories>
This very simple bit of code will run display the title of each top-level category as an h4 (line 2), then build an unordered list from that category’s subcategories (lines 3-7). The output would look something like this:
<h4>Vegetables</h4>
<ul>
<li>Celery</li>
<li>Carrots</li>
<li>Broccoli</li>
</ul>
<h4>Fruits</h4>
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Tomato</li>
</ul>
<h4>Other</h4>
<ul>
<li>Mushrooms</li>
<li>Beef</li>
<li>Salmon</li>
</ul>
The obvious downfall of this basic example is that a category with no subcategories will produce an empty unordered list, like this:
<h4>Empty category</h4>
<ul>
</ul>
While this isn’t a huge problem, it’s definitely not elegant. Fortunately, MT offers a slew of conditional tags to adjust your templates appropriately. For example, the MTHasSubcategories tag could be used to prevent your template from outputting the unordered list if there were no subcategories to list.
All in all, the subcategory features of MovableType 3 are useful and flexible, but it takes a while to fully understand. Hopefully Six Apart will improve the documentation for this particular section so that we can fully take advantage of these powerful features.