Last updated on November 28, 2011. Tags: navigation menu, WordPress page list
WordPress themes typically come with two types of navigation menu, one for static pages and one for categories. Here in Codegrad, for example, the page menu is on the right side of the header while the category menu is within the sidebar. In some cases however, depending on the specifications of the website you're building, you might need more than one page menu.
In such cases, you have two group of pages that you do not want to mix together in the navigation menu. For example, you want the "typical" pages like "Home", "About" and "Contact" in the horizontal bar below the header and the separate pages about your services in the sidebar under the header "Our Services". You want to create two separate navigation menu; one menu for each group of pages.
The list of static pages in a WordPress theme is generated by the following PHP code:
<?php wp_list_pages(); ?>
This code can be usually found in either the the header, the sidebar or wherever the theme creator intends to put the page menu. However, it is usually not found in two places within the theme at once e.g., it's in header or in sidebar but not both.
The first step in having separate page menu is to put this code in areas where you intend to put the page menus. The menus will then include the same set of pages. During this step, you might also want to work on the CSS code if your navigation menu isn't working as expected.
Next, list the post IDs of the pages in a way that they are grouped based on which page menu they will go e.g., list all those that will go to the header together, and those that will go to the sidebar together. Click here to find-out how you can determine the post IDs of static pages.
Finally, for each instance of <?php wp_list_pages(); ?>, insert the include argument indicating the post IDs of the pages that you want to appear in corresponding navigation menu.
<?php wp_list_pages('include=1,11,54'); ?> <!-- menu in header -->
<?php wp_list_pages('include=9,36,51'); ?> <!-- menu in sidebar -->
<?php wp_list_pages('include=9,36,51'); ?> <!-- menu elsewhere -->
Note that the wp_list_pages might have other arguments between the parentheses. In such cases, just separate them with an AND operator ampersand (&) e.g., <?php wp_list_pages('include=11,12&title_li'); ?>.
This method has one disadvantage. You need to manually encode the post IDs of new pages created. Since you are using the include argument in all instances of wp_list_pages, pressing the publish button will not cause the newly published page to appear in the navigation. You need to include its post ID to the menu where you intend it to appear. This isn't practical for WordPress-powered websites that frequently add or remove (non-post) static pages.
This tweak was tested in WordPress 2.8.5. It is likely to work in all nearby versions.
Posted by Greten on April 30, 2010 under WordPress
Comment Rules and Reminders