Mcavity: template recipes

Sorting Categories in Movable Type: Three Solutions

Gautam Patel

If you want to sort your categories in some customized order — i.e., not alphabetically, which is the MT default — there are some solutions. I’ve tracked down three different solutions.

(Note that I am now running MT 3.35, and I’m not sure Solution #3 below works in anything below version 3.3x.)

  1. Use the RegEx Plugin [1];
  2. Use the CustomFields Plugin [2];
  3. Use category basenames [3].

Option 1 : Use the RegEx [4] Plugin  [5]

Here’s what you do:

  1. Grab the plugin (here [6]), and install it. If that link is broken, I’ve got the same thing locally, here [7].
  2. Rename your categories 1FirstCat, 2SecondCat, 3ThirdCat. Note that you can also do 01FirstCat, 02SecondCat, etc. This may actually be preferrable, given the way these things are sorted if you have more than 10 categories. In that situation, the sequence will be 1, 10, 2, 3 …; while, with the double-digit numbering, it goes correctly 01, 02, 03.
  3. Somewhere in your code, add this:

    <MTAddRegex name="RemoveSortNumber">
        s|^([\d\.]*\s*)||   
    </MTAddRegex>
    
  4. Now, when you use the <MTCategories> tag, add the following argument:

    <$MTCategoryLabel regex="RemoveSortNumber"$>
    
  5. Basically, this is stripping digits from the start of the line.

Pros

Cons

Note that much the same thing can be done with the superb MT-Macros [8] plugin. That’s really a three-and-halfth solution, because, as I see it, it’s only a refinement of the RegEx solution.

Option 2 : Use Arvind Satyanarayan’s CustomFields [9] Plugin  [10]

  1. Grab the plugin (here [11]), and install it. Use the latest release, 2.0 RC1. This will not work with the earlier release.
  2. Add a custom category field. Call it something like CategoryNum or some such. Check the required box — this prevents creation of a category without filling in a category number. The category type should be a single line text field so that you can add categories easily later.
  3. I’m not going to get into how you get the custom fields to appear — Arvind’s documentation is exemplary, and bears close study.
  4. Note this, though: the custom category fields do not change the entry editing screen, but the category creation screens.
  5. Now when you create a category you should see a box at the bottom with your custom category. Give it a number (something like 01, 02, etc), in the sequence in which you want your categories to appear.
  6. Now here’s the trick. Over at his own plugin forums [12] at Movalog [13], there’s a thread [14] where Arvind tells of an undocumented sort feature in Version 2.0RC1 of CustomFields [15]. You can add an argument to the MTCategories tag and force a sort by a custom field. Here’s what Arvind says:

    this is a new feature (that I haven’t publiciszed or documented at all for lack of time) introduced with v2.0. To resort by a custom field (called “Foo Bar”), the following syntax is used (on MTEntries, MTCategories or MTEntries):

           sort_by="CUSTOMFIELD:Foo Bar"
                sort_order="ascend|descend"
    

    So supposing you wanted to resort categories by the Foo Bar field in descending order:

           <MTCategories sort_by="CUSTOMFIELD:Foo Bar"   
                sort_order="descend">
    
  7. So in our case, with the CustomField CategoryNum, the code would be:

        <ol>
            <MTCategories sort_by=
               "CUSTOMFIELD:CategoryNum"&
                 sort_order="ascend" 
                 show_empty="1">
                <li>
                    <b><$MTCategoryLabel$></b>
                    <$MTCategoryDescription$>
                </li>
            </MTCategories>
        </ol>
    
  8. Essentially, all you are doing is sorting by the custom field, and then displaying as normal.

Pretty nifty.

Pros

Cons

Option 3 : Use the category <basename> to sort  [17]

This one’s sort of homegrown. Here’s what you do.

  1. Name your categories as in Option 1 [18] above, 01FirstCat, 02SecondCat, etc.
  2. In the category editing screen, unlock the category basename field. It will default to the category label and has the numbers you’ve just given them (if you’re starting from scratch, that is; otherwise skip this step, the basename won’t change).
  3. Strip the numbers from the basename and lock the field.
  4. Remember to change your archive file path specifiers and mappings in the Settings - Publishing - Archive Maps so that your category archives are built with the basename rather than the label.
  5. Now, instead of using the <MTCategoryLabel> tag, you use the <MTCategoryBasename> tag, passing it through any formatting filters (for upper/lower case, etc). For example:

        <ol>
            <MTCategories show_empty="1">
                <li>
                    <a href=<$MTCategoryArchiveLink$>">
                    <$MTCategoryBasename$>
                    </a><br />
                    <Does this work?></li>
            </MTCategories>
        </ol>
    
  6. Basically, you’ve swapped the category label and the category basename. Now MT sorts categories by the label, in the numbered sequence you’ve set up, but uses the basename of each category for display.

Pros

Cons

My choice? Option 2 [21], without a doubt. Arvind rules!

Links and References

[1] RegEx:
http://www.bradchoate.com/downloads/mtregex-1_61.zip
[2] :
[3] here:
http://www.bradchoate.com/downloads/mtregex-1_61.zip
[4] here:
http://www.mcavity.com/files/mtregex-1_61.zip
[5] MT-Macros:
http://www.bradchoate.com/weblog/2002/08/12/mtmacros
[6] CustomFields:
http://plugins.movalog.com/customfields/
[7] :
[8] here:
http://plugins.movalog.com/customfields/
[9] plugin forums:
http://plugins.movalog.com/forums
[10] Movalog:
http://www.movalog.com/
[11] thread:
http://plugins.movalog.com/forums/viewtopic.php?id=581
[12] CustomFields:
http://plugins.movalog.com/customfields/
[13] plugin forums:
http://plugins.movalog.com/forums
[14] :
[15] MT-Macros:
http://www.bradchoate.com/weblog/2002/08/12/mtmacros
[16] the Regex:
http://www.bradchoate.com/downloads/mtregex-1_61.zip

Share:

| | |
More...
all content © . licensed under a creative commons license. some rights reserved.