How to get submenu items from a WordPress menu?

How to get submenu items from a WordPress menu?

To give you an idea of how it works with the default options, imagine you have a menu like this: If you’re on page A, you’ll see B and C in the menu (A is a top level page, so it shows children). If you’re on B or C, again, you’ll see B and C (B and C are second level pages, so it shows siblings).

How to set custom Nav menus in WordPress?

Get the custom nav menus set in WordPress. $locations = get_nav_menu_locations(); You might need to change ‘primary’ if you are copying and pasting this, that’s just what we used in this theme. $menu = wp_get_nav_menu_object( $locations[ ‘primary’ ] ); // ‘primary’ is our nav menu’s name. Get the submenu items of the section we identified earlier.

How to create a secondary menu in WordPress?

The menu builder in WordPress is great, however quite often I find myself needing to create a secondary menu on pages, containing just a subset of the menu. In my case the rules are simple: when viewing a top level page we want to list all child pages nested beneath it.

How to change the Order of menu items?

Supply an orderby field via ‘output_key’ to affect the output order of nav menu items. Default ‘menu_order’. (string) Menu items post type.

How to Filter menu based on direct parent?

By default, if you have many nested levels, this code will filter the menu by the absolute top level parent. If you want the menu to drill down dynamically by filtering the menu based on the direct parent, pass a “direct_parent” => true parameter to the wp_nav_menu call.

How to change the menu name in WordPress?

If you’ve copied and pasted the above code, don’t forget to change Menu Name to the slug or name of your menu. By default, if you have many nested levels, this code will filter the menu by the absolute top level parent.


How to display current parent and its sub menu only?

Then you can display it in your theme using wp_nav_menu (just like you normally would), but also passing in a sub_menu flag to activate the custom sub_menu function: When on a page you first get all the pages, then you can get the current page ID, get the children in an array and loop through this array like this:

How to display submenu ( not as dropdown ) below mainmenu?

The submenu is just an nested unordered list. To change how the menu appear you would have to override the styles applied to them. It would probably be best to start by creating a child theme and doing a rewrite for the menu system in the style.css file.