Wordpress: How to order categories

Looking at my statistics I notice quite a few people find me looking for “Wordpress down down categories” or “Wordpress order categories”. Since the page they find is for creating a drop down box for your Wordpress categories I figured I might as well post how to sort your categories alphabetically in ascending or descending order.

Update: I’m such a dork. I should think through these things more before I go posting. If you really want to read my original solution, go ahead and click the link at the end of this to go to the next page. However, it’s much easier to just edit one little line in index.php:

Line #71 in the default template:

<?php wp_list_cats(); ?>

Change that to:

<?php wp_list_cats('sort_column=name'); ?>

If you want the list sorted descending:

<?php wp_list_cats('sort_column=name&sort_order=desc'); ?>

That’s it! Read more about the wp_list_cats function on the Wordpress Wiki and what arguments you can pass to it. Related: list_cats function on the Wordpress Wiki.

This page of this post is what I originally posted on this subject. It really isn't necessary, but I thought I'd leave it here in case someone wants to see it? ha! Anyway, go to page one of this post to do this the easy way... or keep reading for the unnecessary way.

If there’s a way to sort them in the admin I have completely looked over it. I’m pretty sure it’s not there though. So here’s how I did it:

Before changing anything always remember to make a backup of your files! If you are not comfortable messing with a little bit of PHP code I suggest you ask a friend to do it for you.

In wp-includes/template-functions-category.php

Lines 263 and 264:

if (!isset($r['sort_column'])) $r['sort_column'] = 'ID';
if (!isset($r['sort_order'])) $r['sort_order'] = 'asc';

Change ‘ID’ to ‘name’. If you want it sorted descending, change ‘asc’ to ‘desc’.

Line 282:
function list_cats($optionall = 1, $all = ‘All’, $sort_column = ‘ID’, $sort_order = ‘asc’, $file = “, $list = true, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $use_desc_for_title = 1, $children=FALSE, $child_of=0, $categories=0, $recurse=0, $feed = “, $feed_image = “, $exclude = “) {

Change ‘ID’ to ‘name’. If you want it sorted descending, change ‘asc’ to ‘desc’.

And yes, I know this one doesn’t really matter a whole lot. It’s just changing the defaults. I just throw it in for good measure. If this sentence confused you, just ignore it.

Line 195:
function dropdown_cats($indexpage = false, $optionall = 1, $all = ‘All’, $sort_column = ‘ID’, $sort_order = ‘asc’, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE, $selected=0, $hide=0) {

If you don’t use the Drop down categories code you can skip this one. It also sorts the drop down box in the admin, but I don’t really see that as a big problem for anyone.

Change ‘ID’ to ‘name’. If you want it sorted descending, change ‘asc’ to ‘desc’.

Note that the $indexpage = false, is something I added for the drop down categories code I posted.

And that’s it. Upload wp-includes/template-functions-category.php and you’re set. I hope this wasn’t too confusing. Let me know if you have any problems, questions, or feedback.

← 
 →
Programming