Filter and Hooks, Programming, WordPress

WordPress: Get Menu Items and it’s sub-items in an array

WordPress provides built in function that displays the navigation menus without having trouble to write any code. But, in some cases we may want an arrays of menu items and tweak it.

Following steps are handy for grouping the menu items in  a single array.

1. Register your custom navigation menu.
We have readily available function to register our custom menu, therefore we do not need to call add_theme_support( ‘menus’ ) :

Here, $locations is an associative array of menu location slugs (key) and descriptions (according value). I have registered custom menu ‘primary_menu’ as a key  and ‘Main menu’ is the description for the registered ‘primary_menu’

 

2. Create and add Menu items the dashboard

 

3. Get the array of Menu items

Use the custom menu key ‘primary_menu’ in my case to get the  all the menu items associated with it.

4. Function to the get the array of menu items

You can create your own function to get the menu items and its sub-items in an array

 

Hits: 27

Standard