Upgrade wp_list_categories() to wp_nav_menu()
in Functions
as how-to, WordPress, wp_list_categories, wp_nav_menu
In this third installment in the upgrade to wp_nav_menu() series of posts, I will be dealing with moving from wp_list_categories() to wp_nav_menu().
Let’s start with the same basic outline and list the default options for each function. We will start with wp_nav_menu() 1 first:
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' => ''
) );
We follow with wp_list_categories() 2, 3 which is often wrapped in a <ul class=”menu”> tag:
<ul class="menu">
wp_list_categories( array(
'include' => '',
'exclude' => '',
'exclude_tree' => '',
'child_of' => 0,
'hide_empty' => 1,
'orderby' => 'name',
'order' => 'ASC',
'use_desc_for_title'=> 1,
'number' => NULL,
'hierarchical' => true,
'show_count' => 0,
'pad_counts' => 0,
'style' => 'list',
/* 'style' set to list "creates list items for an unordered list" */
'show_option_all' => '',
'show_option_none' => __('No categories'),
'show_last_update' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'current_category' => 0,
'taxonomy' => 'category',
'title_li' => __( '' ),
/* 'title_li' set to '' for menus from the default 'Categories' */
'echo' => 1,
'depth' => 0,
'walker' => 'Walker_Category'
) );
</ul>
Here is the break-down of the wp_list_categories() default options from above:
'include'– not used'exclude'– not used'exclude_tree'– not used'child_of'– not used'hide_empty'– not used'orderby'– not used'order'– not used'use_desc_for_title'– not used'number'– not used'hierarchical'– not used'show_count'– not used'pad_counts'– not used'style'– not used'show_option_all'– not available'show_option_none'– not available'show_last_update'– not available'feed'– not available'feed_type'– not available'feed_image'– not available'current_category'– not available'taxonomy'– not available'title_li'– not used'echo'– similar to wp_nav_menu(); true versus 1 as value'depth'– same as wp_nav_menu()'walker'– see below for additional notes
Although there are great many options available to be used with wp_list_categoires() most are not used (as noted in the list above) in place of the user interface of wp_nav_menu() found under Appearance | Menu in the dashboard. The options that are marked as “not available” are currently not directly supported by wp_nav_menu() without the application of filters. The last few options remaining match up almost exactly with wp_page_nav() in a similar fashion as was shown with wp_list_pages().
Just like the wp_list_pages() consideration, the wp_nav_menu() fallback_cb option may be set to 'fallback_cb' => 'wp_list_categories'. This backward compatibility may also be addressed with a custom function as was noted, too.
Note: In most cases you would leave the wp_nav_menu() 'walker' option set to its default NULL although wp_list_categories() uses its own default walker class.
- Share this:
- Share




If you coud help me…
wp_list_categories uses li and /li by default.
I need to edit manual in the sidebar.php li and /li because i want to insert a little image, identical on every cathegory row.
Thanks
@Daniel – We offer WordPress Services that would be better suited to addressing your requirement. Please feel free to make use of out contact form to send us additional details. Thanks.
Is wp_nav_menu faster than wp_list_categories since the relationships are pre-assigned for the menu? I would think this would speed the walker up.
@Alex – Logically speaking I would have to say no as ‘wp_nav_menu’ in this case would be using ‘wp_list_categories’ as its fallback. Granted the difference will not likely make any difference to the reader / end-user but I cannot definitely say as I have not done any actual speed tests …
Great thanks. Also, isn’t the benefit of wp_list_categories that you don’t have to worry about updating the menu if you add additional categories? I don’t really understand why moving from wp_list_categories to wp_nav_menu is considered an “upgrade”. What are the benefits anyways?
@Alex – Upgrading to an implementation of `wp_nav_menu` using `wp_list_categories` as a fallback provides more functionality and options for the end-user; although if you are developing a site specific theme you may not have use for the new menu functionality.