The Movable Type User Manual

NOTE: This documentation is for Movable Type 3.2. If you are using a newer version, please see the documentation for Movable type 3.3x or Movable Type Enterprise.

« Creating a Category or Subcategory | Up | Assign Multiple Categories to an Entry »

Chapter 5: Categories

Working with Category Tags

Problems

You need to display category data in your published pages.

Solutions

Use the template tags prefixed with MTCategory.

Discussion

Category tags can be used to display a list of categories and their information in both hierarchical and non-hierarchical forms. These tags can be used in MTArchiveList archive_type="category", MTEntryCategories, MTCategories or MTSubCategories tag contexts. Category tags may also be used anywhere in a Category Archive template.

The basic tags when working with non-hierarchical data are as follows. (For an exhaustive list of Category tags see Appendix A.)

  • MTCategories [show_empty="1"]

    A container tag that loops through the list of categories in your weblog. The optional show_empty attribute forces a category to be displayed even if no entries have been assigned to it. Without it only categories with a count of one or more will be displayed. This list of categories is created without consideration to their hierarchy.

  • MTCategoryLabel

    The category title.

  • MTCategoryArchiveLink

    A link to the archive page for this category. Unless category archives have been enabled, this tag may not be used and will result in an error.

  • MTCategoryCount

    The number of entries in this category.

Here is a sample layout that produces an unordered list of category titles with their count in parenthesis.

  <ul>
  <MTCategories show_empty="1">.
   <li><a href="<$MTCategoryArchiveLink$>">
   <$MTCategoryLabel$></a> (<$MTCategoryCount$>)</li>
  </MTCategories>
  </ul>

The MTCategories tagset produces output for each category without respect to its placement in the hierarchical structure.

The hierarchical structure of categories can be reflected in a template as well. Producing hierarchical category layouts requires the use of a more complex set of tags and interactions.

In MTCategories the example layout has one category where this block is repeated for each. This behavior is similar to that of tagsets such as MTEntries and MTComments. However, category hierarchies where any category can have additional, associated child-categories is not allowed. For this MT uses a technique called recursion, where the layout is repeated inside of itself.

Here are a few of the many tags used to display and interact with these hierarchical category structures.

  • MTSubCategories

    A container tag similar to MTCategories except that it respects the hierarchical structure.

  • MTSubCatIsFirst

    A conditional tag that displays its contents only during the first MTSubCategories loop.

  • MTSubCatIsLast

    A conditional tag that displays its contents once the last MTSubCategories loop has been reached.

  • MTSubCatsRecurse

    The point where a list of children should be displayed using exactly the same format defined in MTSubCategories.

Let's look at an example. The following will produce nested, unordered lists that display the category hierarchy:

  <MTSubCategories> 
  <MTSubCatIsFirst><ul></MTSubCatIsFirst>
  <li>
  <$MTCategoryLabel$>
  <$MTSubCatsRecurse$>
  </li>
  <MTSubCatIsLast></ul></MTSubCatIsLast>
  </MTSubCategories>
  

In this example, MTSubCategories loops through a block of template code. When it reaches MTSubCatsRecurse the block is then repeated for each category for which the current category is a parent. MTSubCatIsFirst and MTSubCatIsLast are used to output the necessary HTML tags for displaying a hierarchical list.

Submit Feedback on This Article

Your comments on how we can improve this article are appreciated; but please do not use the feedback form to submit support requests or question. We will not respond to or publish such queries submitted through this form. If you have a technical question or problem, visit Movable Type Support.

Six Apart
Makers of weblog software and services for individuals, organizations and businesses.
This website is powered by Movable Type.