cc licensed ( BY NC ND ) flickr photo shared by ailatan

With a relaxing day of nothing definite to do, I decided to poke around the corners of the blog house and clean up some dust gathered in the corners of my theme. Themes that predate WordPress 3, like mine, do not take advantage of the newer menu editing options.

Old school themes were typically set up to turn Pages into menus, for example mine is wedged inside the header.php template:

where I have picked the pages listed by their id numbers to appear. This is rather tedious to edit and manage, and I also ran into issues because I have a few custom php pages that draw in the wordpress theme, and thus I end up with about three variations of header.php that would need edits.

The first logical step is to pull out the navigation elements into its own php file (the bits above) that I call nav.php. This way, in any header file, I pull that in via a simple replacement of above with

Now I use the WordPress Menus editor (under Appearance on the dashboard). The great thing abut the menu editor is you can have more than just pages- you can use categories, and you can also mix in custom (e.g. outside URLs) all in one menu.

I created one i called, for lack of better terms, “doggie”

(click for full size)

I have the blog main URL as a custom one, a few of my pages, and a few external sites. In the menu pane on the right, you can slide the items around to get the order you like. It is very “widgety” and most current themes likely use widgets to do this.

To use this in my theme, I followed the options for inserting a menu listed in the codex for wp_nav_menu() function, so my nav.php file now reads:

which simply tells my theme to use the doggie menu. The value for container tells it not to wrap it in a dv as it is already in your theme. It even seems to take care of theming the menu as “selected” if you are on the page that matches the menu item (e.g. my custom page that lists my 100 last posts)

Your own integration depends on how your themes menus are set up in the template. Being able to edit the menu in the wordpress dashboard is not only easier, but much more flexible than the old school pages approach.

This turned out to be a useful exercise for another project, where the codex page for the menu function includes sample code that can have different menus for logged in and not logged in users:

Loving the WP hacking…

If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
Become a patron at Patreon!
Profile Picture for CogDog The Blog
An early 90s builder of web stuff and blogging Alan Levine barks at on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person. And he is 100% into the Fediverse (or tells himself so) Tooting as

Leave a Reply

Your email address will not be published. Required fields are marked *