Help - Search - Members - Calendar
Full Version: Sorting MTCategories
Movable Type Community Forum > Additional Resources > Tips and Tricks
craig1972
In my index template, I want to display all categories, and then all entries underneath.

Here is what I have:

CODE
<MTCategories sort_by="MTCategoryID">
....
</MTCategories>


The "sort_by" doesn't seem to be working! I have tried "category_id", "id" etc.

Help! How should I sort the listing of categories which is currently by default based on the category title? I want it to be by category ID.

P.S. Manual didn't mention any specific "sort_by" for the MTCategories. Is this possible?

Thanks!
cutterchgo
I've been using a neat trick for sorting categories that I found on here somewhere. It requires the regex plugin.

Simply, add numbers to your category names. For example, if you have three categories named:

cat1
cat2
cat3

rename them to

1cat1
2cat2
3cat3

Now, they will appear in the order (1,2,3) but they have that ugly number in front of them. That's where the regex tip comes in:

First, you'll need to install the regex plugin from www.bradchoate.com.

Then, in your template, add the following:
CODE
<MTAddRegex name="RemoveSortNumber">s|^([\d\.]*\s*)||</MTAddRegex>

This sets up the expression to be applied to your MT tags.

Next, in every case where you wish to output the category name, use the following:

CODE
<$MTCategoryLabel regex="RemoveSortNumber"$>


Or some variant. Just be sure to include the "regex='RemoveSortNumber'" argument to apply the expression that removes the number from the beginning of your category. I am using this to control the sort of all of the pages in the site I am working on. It's a bit weird at first, but once you get it setup, it's a breeze to work with.[CODE]
craig1972
Thanks, this is useful, but to be honest I have thought of this.

I was wondering if sorting (which is a BASIC feature!) is possible in a cleaner fashion?

Thanks!
cutterchgo
I don't think so. When I was scouring for it it seems that categories are only sortable alphabetically. I couldn't find any plug-ins that added this ability outside of the solution I posted and a more complicated one using the lists plug-in to put the categories in a list and THEN sort them (My guess is that you'd have to use key value pairs to identify the position in this case and have the list plug-in sort the records by that value rather than name. It seemed more complicated so I didn't bother trying it.

Anyone else have any ideas? A simpler one definitely would make life a bit easier.
imabug
QUOTE
The "sort_by" doesn't seem to be working! I have tried "category_id", "id" etc.


Probably a sign that sort_by isn't a valid attribute for MTCategories.

I don't see any sorting plugins off hand, but if your blog is SQL based, something that might help is the SQL plugin. set up your SQL query to sort the list the way you want. Don't know if you'd be able to nest a MTEntries block in it though.
craig1972
Thanks guys.

Cutter, thanks for that code, I have finally settled with it except that I am using brad's MTMacro instead of the regex, that easily wins my "Favorite Plugin" awards hands-down.

I have one additional question. So my categories now look like they should:

CODE
 CATEGORY 1:

 - Entry 1: This is good
 - Entry 2: This is super

 CATEGORY 2:

 - Entry 1: That is good
 - Entry 2: That is super
 - Entry 3: That is super cool


Now, I want to have the same sort of style when a user searches through my blog. E.g., if the search is for "super", I want to show the results as follows:

CODE
 CATEGORY 1:

 - Entry 2: This is super

 CATEGORY 2:

 - Entry 2: That is super
 - Entry 3: That is super cool



But I am not sure what I should include in the search template for this to work! The "MTSearchResults" tag doesn't seem to have any ArchiveList tag at all!

Any ideas? Thanks!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.