Hard-code your navigation and get over it
Posted on 23 September 2009 • 66 comments
There’s a question I get asked all of the time and it surprises me every time I hear it.
How do you create your navigation when you’re using WordPress? How do you control the order of the items when you have to use functions like wp_list_pages and wp_list_categories?
The answer is simple: hard-code your nav!
You see, when using WordPress, we seem to get caught up in a particular mindset: one that says, “I must make everything updatable through the back-end.” And sure, when you’re creating a theme for public use or a for site that’s going to be changed constantly by a client, that’s a sensible approach to take. But what about your personal site, where you’ll be maintaining everything and writing all of the templating code anyway? What about your clients’ site where you’ll be the webmaster, and where updating the nav structure will be a simple HTML edit every six months?
I touched on this subject very briefly in response to an audience question when I spoke at the FOWD Tour in Bristol the other week and I’ll be expanding upon it again when I return to the subject for FOWD New York in November. There are times when it’s simply not practical to jump through all those CMS templating hoops just to achieve something that would take seconds to hard-code. So seriously, go out there and hard-code your nav. Stop worrying and get on with it.
But if you insist
For those of you who do want to create your navigation with WordPress’ built-in functions and therefore keep things as dynamic as possible — but want to control the ordering to some degree — here’s a solution you might be interested in, as suggested to me via email by David Smith:
<?php wp_list_pages('include=7&title_li=&depth=1'); ?>
<?php wp_list_categories( $catargs ); ?>
<?php wp_list_pages('exclude=7&title_li=&depth=1'); ?>
Although there’s some debate about whether making two database calls with
wp_list_pages is a good idea, it’s certainly a solution, and demonstrates that WordPress can be relatively flexible when using include and exclude arguments.