Upgrade wp_page_menu() to wp_nav_menu()
in Functions
as how-to, WordPress, wp_nav_menu, wp_page_menu
WordPress version 3.0 has been live for a while and a lot of people want to make use of the wp_nav_menu() functionality but they are using one of the older template tags: wp_page_menu(); wp_list_pages(); or, wp_list_categories().
This post will be dealing with function arguments in common between wp_nav_menu() and wp_page_menu(). Follow-up posts will be covering wp_list_pages() and wp_list_categories(). I will not be covering the specific use for each function’s arguments; the idea is to help show how these options correlate to one another.
Let’s start with the full default of wp_nav_menu() 1 explicitly displayed:
wp_nav_menu( array(
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => '',
'theme_location' => ''
) );
Followed by the same explicit defaults of wp_page_menu() 2, 3:
wp_page_menu( array(
'sort_column' => 'menu_order, post_title',
'include' => '',
'exclude' => '',
'show_home' => false,
'menu_class' => 'menu',
'echo' => true,
'link_before' => '',
'link_after' => ''
) );
Now onto updating wp_page_menu() to wp_nav_menu(), which turns out to be rather simple.
Here is the break-down of the wp_page_menu() default options from above:
'sort_column'– not used'include'– not used'exclude'– not used'show_home'– not used (see bonus section below)'menu_class'– same as wp_nav_menu()'echo'– same as wp_nav_menu()'link_before'– same as wp_nav_menu()'link_after'– same as wp_nav_menu()
First off ‘sort_column’, ‘include’, and ‘exclude’ are replaced by the end-user’s choices using the UI of wp_nav_menu() found under Appearance | Menu in the dashboard. The balance of the default options are the same in wp_nav_menu(), with the exception of 'show_home' => false which is the “bonus” content.
Upgrading from wp_page_menu() to wp_nav_menu() may be as easy as a simple find and replace operation.
add_theme_support( 'menus' );
- Share this:
- Share



Or just make a custom link and add “index.php” in the URL field and “Home” in the Label field
@Andrei – Thanks, I did mention that as part of the “Bonus” section at the end of the post.
“You may need to edit your theme to remove the orginal “home” link if one was being generated before upgrading.”
Where can I edit this?
@chaz – That would depend on your theme. You might start looking in your header.php template file for obvious references to home, then proceed to your functions.php file if necessary.
Gotta love Custom Menus… I’m running into a problem where they are breaking on category.php pages but they work great toerhwise.
@Devin – What issues are you seeing on your Category template pages?
“‘show_home’ => true” works for me:
wp_nav_menu(array('show_home' => true,
));
@Steven – Yes, it works quite well, but I was referencing the parameter `show_home` is not used directly by `wp_nav_menu`. Sorry, it was not clearly indicated above.
You can make wp_page_menu() show ‘HOME by adding the following code to functions.php within your theme:
function my_page_menu_args($args) {
$args['show_home'] = true;
return $args;
}
add_filter('wp_page_menu_args', 'my_page_menu_args');
The problem is that wp_page_menu() does not allow the change of the container element from <div> to <nav> for example. Whereas, wp_nav_menu() does allow the change of the container. So as a fallback, wp_page_menu() might break the CSS styling.
edit: added missing elements as noted. The Doctor
*
from <div> to <nav> which is missing from my previous comment
@Ziad – You bring up a good point, in that `wp_page_menu` by itself may not be a good fallback function for `wp_nav_menu` on its own, but it can easily be incorporated into a function that does address the container issue as it is relevant to a specific theme; and, that function can subsequently be used as the callback instead.