A quick how-to for WordPress users.
When I rolled my book support Web site, LangerBooks.com, into this site — my personal site and blog — I needed to make a clear distinction between the categories that I maintained to support my books and those that were part of my blog. I wanted to make it very easy for readers looking for more information and support to find the information they sought. I achieved this goal by displaying the two groups of categories separately, each under its own heading.
Check out the sidebar to see for yourself. The Book Support heading lists all book support categories (one per title that I support). A list of links appears under the Publisher Links heading that comes next. Then the Maria’s WebLog heading lists all categories that are part of my WebLog.
I could think of two ways to do this with WordPress. Unfortunately, I chose the hard way.
To get the category list in the sidebar, I included the wp_list_cats tag in the sidebar.php file. (Forgive me for not going into detail about that — Miraz and I cover it in detail in our upcoming WordPress book.) I prefer wp_list_cats over list_cats because I find it easier to enter the arguments in the single string format it offers.
The first (hard) way to separate the category list into two groups is with the exclude argument. For example,
wp_list_cats('sort_column=name&optioncount=0&exclude=10,15')
instructs WordPress to list all categories, sorted by name, without post count, except categories 10 and 15. To make this work, I had to construct two separate commands: one for Book Support that excluded the Maria’s WebLog categories and one for Maria’s WebLog that excluded the Book Support categories. At the time, this seemed to make sense, since I was not using a category/subcategory structure and I didn’t expect to add any more categories. In reality, I was just being lazy — and I paid for it.
Of course, I did add more categories. And what do you think happened? The new category was listed under both headings because it hadn’t been excluded from either one. So every time I added a category, I had to tweak one of the two lines of code in the sidebar.php file. Not exactly something I wanted to remember to do.
So I did it the smart (right) way. I created two new categories: Book Support and Maria’s WebLog. Then I went into the Manage Categories administration panel and I edited every category (other than the two new ones) so it was a subcategory of one of the new ones. Finally, I went back into the sidebar.php file and replaced the two original wp_list_cats lines with a line like this:
wp_list_cats('optioncount=0&child_of=35&sort_column=name')
This says to list all categories, without a post count, that are the child (subcategory) of category 35, sorted by name.
Now every time I add a new subcategory, all I have to do is immediately assign it to the correct main category. It will appear under the correct heading. Quicker, easier, and it works.
Of course, you can split your category list by as many parent categories as you like. You don’t even have to list them all in the sidebar. This seems to be the best way to do it. Give it a try and see for yourself.
Oh, and by the way, you can learn more about the wp_list_cats tag in the WordPress Codex. The Codex is an excellent source of information about WordPress with just two tiny problems: some information is a bit tough to find and other information isn’t exactly up to date. But the template tag information is right on target and easy to find.













3 responses so far ↓
1 The Crayfish Project » Blog Archive » Sorting the categories // Feb 1, 2007 at 5:24 pm
[...] I had to put the genera of the three families under individual categories. Maria Langer site I stumbled accross just what the gremlins said I would. [...]
2 The Frosty // Jun 21, 2008 at 9:44 am
Thats cool,
but wp_list_catshas been deprecated. and you should now usewp_list_categories.But thanks, it got me started in the right direction. I am trying to implement CSS3 to get the category’s to split into columns.
:)
3 Maria Langer // Jun 22, 2008 at 9:23 am
Since this post is over two years old, it doesn’t surprise me that it’s content is outdated. I suppose I should go through all these how-to posts and weed out the ones that don’t work anymore.
Thanks for the correction. I hope it helps other readers.
Leave a Comment