[How to] Change Dropdown Menu Depth and Order Using Filters
With the 1.5.8 release, I added dropdown_menu_defaults filter to the dropdown menu widget, so anyone can edit dropdown menu defaults so easily. Here in this post I will show you how to change depth and page order using this filter.
First open functions.php
in your theme folder and add those lines:
function my_dropdown_defaults($menu){ // Change settings here return $menu; // return the variable array back } add_filter( 'dropdown_menu_defaults', 'my_dropdown_defaults' );
This is the basic structure of a filter. We attach this filter to a tag called dropdown_menu_defaults
so whenever this filter is called our function will run.
Next step we will change depth of menu. By default, depth is 4, here i will limit it to 2, so that only one level dropdown will show up. We can do it easily adding this line
function my_dropdown_defaults($menu){ // Change settings here $menu['depth'] = 2; return $menu; // return the variable array back } add_filter( 'dropdown_menu_defaults', 'my_dropdown_defaults' );
Now let’s change order of the pages on the menu. Dropdown menu widget uses wp_list_pages defaults to list your pages. So any setting you see on the codex page is acceptable for the dropdown. Let’s sort pages by IDs:
function my_dropdown_defaults($menu){ // Change settings here $menu['depth'] = 2; $menu['sort_column'] = 'ID'; return $menu; // return the variable array back } add_filter( 'dropdown_menu_defaults', 'my_dropdown_defaults' );
Now whenever a dropdown is generated this function will set depth to 2 and change sort column to page ids.
Please note that this example is given for pages menus. For category dropdowns you need to use wp_list_categories defaults and for wp3 custom menus you have to use wp_nav_menu defaults. Generally defaults like order, depth, and sort_column remain same, but you may find differences between arguments.
Btw. I also added a dropdown menu themes page so you can view all themes without switching anything. Check them out here.
Cheers.