FAQ: Sorting the Categories and avoid alphabetical Sequence

Question

I have a couple of categories. When using the MTCategories tag, these are sorted by name. However, I preferred if I could choose the order manually. There is a logical order of the categories, and the categories should be listed in that order. Can it be done?

Answer

Movable Type does not have a built-in function for achieving an arbitrary ordering of the category list. We have to use a plugin for that.

The idea for implementing the ordering is as follows: we will prefix all category names with some text, which is removed right before the category name is output to HTML. As an example please look at my top-level categories in the sidebar. These are...

  • Basics
  • Installation
  • Configuration
  • Plugins
  • FAQ
  • Problems
  • Movable Type Weblog

As you can see, these names are not sorted alphabetically. They are sorted in an order, which I regard as reasonable. If you could open my Movable Type installation, you would see that the names of the categories actually are...

  • 10Basics
  • 20Installation
  • 30Configuration
  • 40Plugins
  • 50FAQ
  • 60Problems
  • 70Movable Type Weblog

Each category name has been prefixed with a number. When sorting the category list, Movable Type looks at this full name and sorts the categories in the way that I want. I only have to take care of the leading digits being removed, before the category name is output to an HTML file.

A list of all top-level category names can be created by the following tag code. The name of the category is the result of the MTCategoryLabel tag.

<ul>
<MTTopLevelCategories>
<li>
   <a href="<MTCategoryArchiveLink>">
      <MTCategoryLabel>
   </a>
</li>
</MTTopLevelCategories>
</ul>

Fortunately, Movable Type allows modifying the result of a tag by additional attributes. These attributes are implemented by plugins written in Perl. They implement some function, taking an input parameter, which is modified and returned as the function's result. For example, in Filename with Underscore or Dash? I showed the »dirifyplus« plugin, which was used for modifying the filename of a generated HTML file.

Here we are looking for a function "take a string as an input parameter, remove all leading digits, then return the resulting string". We will use the Regex Plugin for that task. It is a general purpose plugin, allowing all kinds of "search and replace" functions.

After having downloaded the plugin from the above location, you have to copy some files to your Movable Type installation. These files have to be distributed as follows.

[MT]/plugins/regex.pl
[MT]/extlib/bradchoate/regex.pm
[MT]/extlib/bradchoate/postproc.pm

After having installed the plugin, we will now create a named regular expression. This expression has to be defined in each template, where a category name is used. As there are probably many such places within your weblog, it is a good idea to create a module template and include that module template from several places. If you are unsure about module templates and including a template, please read Organize your Templates with MTInclude.

The tag for creating the named regular expression is as follows (should be all on one line).

<MTAddRegex
   name="RemoveLeadingDigits">s|^([\d\.]*\s*)||</MTAddRegex>

So create a module template, name it »module-regex«, and insert the above line into it. Then in all templates, where a category name is used, insert the following MTInclude tag somewhere near the top of the file.

<MTInclude module="module-regex">

Now your templates know about the named regular expression, and we can use it. Replace the code for creating the list of categories with the following.

<ul>
<MTTopLevelCategories>
<li>
   <a href="<MTCategoryArchiveLink>">
      <MTCategoryLabel regex="RemoveLeadingDigits">
   </a>
</li>
</MTTopLevelCategories>
</ul>

Here the category name is output in a modified form. The named regular expression »RemoveLeadingDigits« is applied.

Summarizing, by prefixing the category names with some digits, we can sort the list in any order that pleases us. Movable Type will respect the digits when sorting. So the list is, as we want it. However, if the name of a category is to be output, we do not want the digits to appear. That's why each occurrence of the MTCategoryLabel tag has to be modified with our named expression.

As a positive side effect, we can now use the same category name several times in the category tree. Movable Type does not allow duplicate category names, even if the two categories are under different parent categories. By prefixing the name with the digits, we can make all names distinct, although they look identical to the user.

mgs

Feedback is welcome!

What do you think about this entry? Was it interesting or boring? I would like to hear your comments. If the text was helpful, please consider setting a link to http://www.movable-type-weblog.com/.

No spam please!

For protecting this weblog I have installed the MT-Approval Plugin. You have to view a new comment in preview mode, before it is saved on the server. Moreover, I will view your comment manually, before it is published. You can find more information on the subject in the entry Weblog Spamming Basics.

With an active TypeKey session, your comment will be published immediately.

Post a new comment

TypeKey has temporarily been disabled at this location. Please create your comment without using TypeKey or log in from the preview dialog.




Remember Me?


Comment

Vix | June 3, 2005 12:51 PM

That's ingenious. Thanks for that.

I'm wondering though, how do I prevent my permalink from showing the category as 10CategoryA?

Update by mgs: Question has been discussed by private email.

Comment

Gary | June 14, 2005 01:16 PM

I have accomplished the same effect (control the order of categories displayed) by simply putting the numbers within html comment tags, which are then ignored but force the order I want. (I tried to post an example, but it keep nuking the characters. Essentially it's "left bracket/question mark/dash/dash/10/dash/dash/right bracket")

Works the same without the extra coding you suggest (in fact, no coding/plugin whatsoever).

Cheers,
Gary

Comment

Chris | August 1, 2005 10:49 PM

Thanks, that's a really useful idea...

However, I have the same question as Vix. My permalinks show 10CATEGORY etc.

I also tried the solution Gary posted below, which works just fine for all the on-page stuff and even permalinks, however it fails for page titles ("left bracket/question mark/dash/dash/10/dash/dash/right bracket"CATEGORY).

I don't care which work-around to use, but I'd like to have custom-sorted categories that are displayed without prefix
1) on-page
2) in permalinks
3) in page titles

I'd appreciate your input.

Comment

Michael G. Schneider | August 2, 2005 07:51 AM

Chris, are you sure that the regex="RemoveLeadingDigits" has been inserted in all place where a category name is used? You also have to do so in the "Edit Archive File Options" dialog.

For example, in my installation of Movable Type the Archive File Template for the Category Archive Type reads...

category/<MTArchiveTitle dirifyplus="pld" regex="RemoveSortNumber">.html

Michael G. Schneider

Comment

Chris | August 3, 2005 06:56 PM

Michael, thank you for your help. Still haven't been able to figure it out though.

The problem are category links, and not permalinks as I had said before.

http://www.a.com/blog/archives/20firstcat/30subcat/index.html
http://www.a.com/blog/archives/40secondcat/index.html

All entry permalinks work fine:
http://www.a.com/blog/archives/some-entry.html

I checked my weblog config -> archive files and the category archive field is empty.

I tried entering a couple of things that included regex="RemoveLeadingDigits", but it didn't work. I got stuff like:
http://www.a.com/blog/archives/second cat/
No leading digits, but not the way it's supposed to be either.

Also, I'm not sure because in some instances there are subcategories like
http://www.a.com/blog/archives/20firstcat/30subcat/index.html
but in others there are not.

What I would like to get are category links like
http://www.a.com/blog/archives/firstcat/subcat/index.html

Thanks for helping.

- Chris

Comment

Michael G. Schneider | August 4, 2005 10:57 AM

Hello Chris,

I tried to reproduce your scenario and I think I could make it work. As the Archive File Template for the Category Archive Type I wrote (all on one line)...

<MTAddRegex name="RemoveLeadingDigits">s|^([\d\.]*\s*)||
</MTAddRegex>
<MTParentCategories glue="/">
<MTCategoryLabel dirifyplus="sld" regex='RemoveLeadingDigits'>
</MTParentCategories>/index.html

Then the directory structure was created correctly. All links worked fine.

As you see above, I repeated the definition for the RemoveLeadingDigits inside the Archive File Template. This may be necessary in certain situations. I suspect that Movable Type has not yet seen the definition in the template, if the filename is built.

However, in my Movable Type Weblog the definition only appears in the template. There I do not have a problem.

Michael G. Schneider

Comment

Chris | August 4, 2005 11:36 AM

Wow, Michael, I'm amazed :-)! It's working perfectly.
Thanks so much!

Comment

julian | November 6, 2005 02:20 AM

Hello,
First of all, thank you very much for this article! I just posted another example with modified regex on my blog. Hope it would be of help for all interested in the subject.

Comment

ady | May 5, 2006 08:46 AM

For the include... , can I do a php include instead?

Comment

ady | May 5, 2006 09:29 AM

Great plugin. I just implemented it but am having problems with the Category page. I included the following line of code: MTCategoryLabel regex="RemoveLeadingDigits" between the Title tags but it don't work. However, that worked on the Individual page. Any ideas on how to resolve this?

Comment

Lutz | July 14, 2006 06:21 PM

This is what i was looking for!!
Besides that, i have to give you a very big compliment for your instructions...
usefull, short, working
Thanks!

Comment

rj | August 23, 2006 10:33 PM

I just used your tutorial and it's GREAT!

one thing to point out, though.

in the new version of MT (or at least the version I downloaded yesterday), you're able to change the way a category name is displayed while keeping the way it s hows up in the archives (or the reverse).

so if you want to make basic, config, and install categories, ou can go do that, then rename the way they're displayed as 10basic, 30config, 20install, or whatever. your list will sort with the numbers, and the archive paths themselves will still be basic, config, and install.

hope this makes sense!

Comment

Mat Toor | February 13, 2007 06:23 PM

I wonder if someone can help. I'm using MT 3.34 and can only get this solution to work up to a point. I get the Categories displaying on the main index page correctly - and the directory structure looks right on the Category index pages. (ie the url doesn't show the initial numerals). But when I click on the category link to go to a category archive page I see the numerals in the title.
See http://www.mathewtoor.co.uk/denofwii/reviews/
I'm quite a newbie and I can't seem to find things like "Edit Archive File Options" for instance.
My "Config | Categories: area has tabs for Indexes/Archives/System/Modules - but none seem to have the stuff mentioned here. Should I actually be looking elsewhere? Any guidance gratefully accepted. Mat