Using both Pages and Categories for WordPress’ Page Menu

A lot of WordPress themes made use of the handy WordPress page_menu() function that generates the main page horizontal page navigation bar at the top of the website. However, there is one annoying little restriction this handy functionality comes packaged with, namely:

How do I use both pages and categories for the main navigation page menu bar at the same time?

Frustrating, really it is.

However, after digging around a little I have come up with a little hack that solves this dilemma for us, though be warned, you purists out there are not going to like it – after all, it does involve editing a core WordPress file – meaning in essence that should you upgrade your WordPress installation you’ll most likely have to add in this hack all over again.

But hell, if you don’t mind that then read on a little – after all, as you can see above on my blog’s menu, it seems to be working damn well thank you very much! :)

So here goes. First, locate the post-template.php file that will be sitting directly in your /wp-includes folder. Open that file and browse to line 836, where you should come across a line that reads:

$menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );

What this line is doing is basically building up the return menu string by calling the built in wp_list_pages WordPress function. So our trick is going to be to extend that returned menu string by adding on our category listing to the end of it in the correct format that it expects.

And these are the lines you need to add directly after the line above:

$list_args['include'] = '17,77,39,21,80,46,27,4' //where these are valid catergory IDs;
$menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_categories($list_args) );

As you can see, all we are doing is making use of WordPress’ wp_list_categories call which we’ve extended by asking to include some specific categories only. (Of course, we could skip this step by leaving out the first of our added lines, or perhaps change it to exclude certain categories by changing the call to ‘exclude’).

In any event, saving the changes to our file and uploading it back to the server, you should now find that on reloading your website that all of a sudden you have both pages and categories listed side by side in the main navigation menu.

Nice! :)

You might also enjoy:

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code, Tutorials and tagged , , , , , . Bookmark the permalink.
  • http://profanebeatz.com Profane

    this should work by logic, but unfortunately it’s not for me.
    also the semi-colon at the end of “$list_args['include']” line should be after the set of numbers, not after “//where these are valid catergory IDs” for anyone who might come across this post.

    i was trying this on WordPress 2.8.5 with the Widescreen Theme

blog comments powered by Disqus