Movable Type: Displaying categories in non-alphabetical order

February 6, 2004

I’m working on that all-MT site (coming along *beautifully* I might add). It should be “live” as soon as this weekend.

I ran into a problem, which I solved, that I need to blog otherwise I’ll never remember how I did it.

The problem: MovableType by default lists categories in alphabetical order. You can see it here on my blog in the sidebar. There’s no out-of-the-box way around that.

I’m going into geek wanna-be mode, so if you don’t have a Movable Type blog and/or you don’t care about customizing template code you probably want to bail now.

Still with me? Good.

So your basic:

<MTCategories>
<h1><a href =”<$MTCategoryArchiveLink$>”><$MTCategoryLabel></a></h1>

</MTCategories>
<MTEntries lastn = “15″ sort_order = “descending”>
<ul>
<li><a href = “<$MTEntryPermalink$>”><MTEntryTitle></a></li>
</ul>
</MTEntries>
</MTCategories>

is going to list those categories in A-Z order with the titles of the entries in that category underneath. But what about if you have a page where you want to list your categories in a different order, maybe according to importance or some other criteria than where the name falls in the alphabet?

The answer, that I found on this page, is solved by Brad Choate’s Regex plug-in.

Essentially, I had to “trick” Movable Type into listing the categories in my order, yet let it think that the entries were in the order it wanted.

First, I added “CatA:” “CatB:” “CatC:” to the category names to define the order I really wanted them.

Then the code below defined that pattern and stripped it out before entry display. Do I know much about regular expressions? No. But I was able to take the example from the other page and reconfigure it to my liking and it worked!

The final, working code:

<MTRegexDefine name=”patt1″>s/Cat.: //gi</MTRegexDefine>
<MTCategories>
<MTIfMatches var=”CategoryLabel” pattern=”m/(Cat.:)/i”>
<h1><a href = “<$MTCategoryArchiveLink$>”><$MTCategoryLabel regex=”patt1″$></a></h1>
<MTEntries lastn = “15″ sort_order = “descending”>
<ul>
<li><a href = “<$MTEntryPermalink$>”><MTEntryTitle></a></li>
</ul>
</MTEntries>
</MTIfMatches>
</MTCategories>

If you have any questions or comments about this approach, feel free to comment. :-)

Related posts: