Skip to content

Custom Category Sorting

nick edited this page Apr 25, 2011 · 3 revisions

With the Order Plugin

Note: This recipe requires the Order plugin and Custom fields which can be found in the Professional pack

The advent of custom fields held great promise for finally allowing us to sort categories. Unfortunately, only mt:Entries currently supports the use of custom fields as either attributes (e.g. <mt:Entries field:neato="1">) or as a permitted value for the sort/sort_by attribute (e.g. <mt:Entries sort_by="field:hotness">)

So, in the meantime, here's what you can do today to sort your categories.

  1. Create a Category custom field called sort priority (screenshot). Take note of the field's template tag.
  2. Go to category editing page for each category and put in priority order value (screenshot). I recommend specifying values that are factors of 10 to start with (10, 20, 30, etc). This will allow you plenty of room for shifting things around without having to renumber the other categories
  3. Install Mark Paschal's wonderful Order plugin.
  4. Use the following template code methoology (which assumes that the lowest priority given is 10 since the loop variable doesn't work with Order):
    <mt:CategoryBasename setvar="current_cat"> 
    <mt:Var name="default_priority" value="999999">

    <mt:Order sort_order="ascend">

        <mt:OrderHeader>
            <ul>
        </mt:orderheader>

        <mt:TopLevelCategories sort_by="label" show_empty="0">

            <mt:OrderItem>
                <mt:CategoryDataSortPriority _default="$default_priority" setvar="order_by">
                <li<mt:If tag="CategoryDataSortPriority" eq="10"> class="first"</mt:if>>
                    <a href="<mt:CategoryArchiveLink />"
                        <mt:If tag="CategoryBasename" eq="$current_cat"> class="current"</mt:if>>
                        <mt:CategoryLabel>
                    </a>
                </li>
            </mt:orderitem>

        </mt:toplevelcategories>

        <mt:OrderFooter>
            </ul>
        </mt:orderfooter>

    </mt:order>

By Handling Parent and Subcategories Differently

The use case was specifically this: the parent categories were years (2009, 2008, 2007, etc), and the sub categories were locations. Years should be sorted in descending order, while locations are alphabetic in ascending order. By sorting the top-level categories (years) one way, then subcategories (locations) another way, I get the desired result

<mt:IfArchiveTypeEnabled archive_type="Category">
<div class="module-list module">
    <h2 class="module-header">Category Archives</h2>
    <div class="module-content">
        <mt:TopLevelCategories sort_order="descend">
            <mt:SubCatIsFirst><ul class="module-list"></mt:subcatisfirst>
            <mt:IfNonZero tag="MTCategoryCount">
                <li class="module-list-item-2"><a href="<$MTCategoryArchiveLink$>" title="<$MTCategoryDescription$>"><mt:CategoryLabel></a>
            <mt:Else>
                <li class="module-list-item"><strong><mt:CategoryLabel></strong>
            </mt:ifnonzero>
            <mt:SetVarBlock name="curcat"><mt:CategoryLabel></mt:setvarblock>
            <mt:SubCategories category="$curcat" sort_order="ascend">
                <mt:SubCatIsFirst><ul clas="module-list"></mt:subcatisfirst>
                <mt:IfNonZero tag="CategoryCount">
                    <li class="module-list-item-2"><a href="<mtCategoryArchiveLink>" title="<mt:CategoryDescription>"><mt:CategoryLabel></a>
                <mt:Else>
                    <li class="module-list-item"><strong><mt:CategoryLabel></strong>
                </mt:ifnonzero>
                    <mt:SubCatsRecurse>
                <mt:SubCatIsLast></ul></mt:subcatislast>
            </mt:subcategories>
                </li>
            <mt:SubCatIsLast></ul></mt:subcatislast>
        </mt:toplevelcategories>
    </div> 
</div> 
</mt:ifarchivetypeenabled>

Category: Template Tag Recipes

Clone this wiki locally