It’s been a long time since last time I wrote about technical thing on this blog! But now I come back with a small tip of getting all registered menus in WordPress. Just a note: get all registered menus, not menu locations (for menu locations, WordPress already has a built in function –
get_nav_menu_locations()). This is quite useful in some cases when you want to assign a custom menu for a page (like for onepage template – similar to what I did for our music and band WordPress theme).
The code snippet is very simple as the following:
$menus = get_terms( 'nav_menu' ); $menus = array_combine( wp_list_pluck( $menus, 'term_id' ), wp_list_pluck( $menus, 'name' ) ); print_r( $menus ); // You'll get: // array( 'menu_id' => 'Menu Name' );
There are one interesting in the code above: each menu is a term of a taxonomy
nav_menu. So it will have a full list of attributes that a normal term has, like:
name, etc. In this code, I only get
name and combine them in an associated array, and that’s enough for use.