Using both Pages and Categories for WordPress’ Page Menu CodeUnit 15 MAR 2010

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! :)

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • 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

  • Internet Guy

    Worked fine with me…but how on this earth I can show the child categories within (maybe as a dropdown menu) ??? It shows only level menu with the above hack…

    • Very, very old post. Hack not required any more thanks to WordPress’ native
      menu object nowadays. Probably best to disregard.