0

I want to be able to get an array of all my pages in my chosen menu along with all it's sub menu items.

Using wp_get_nav_menu_items returns a single dimensional array with only the top level menu items.

Using wp_nav_menu returns HTML which isn't so helpful as I am using bootstrap so want to create the HTML structure myself.

3
  • you can use this filters to edit the menu elements : developer.wordpress.org/reference/hooks/wp_nav_menu_items. if you don't want to change the datas but only the render of the menu, you need to create a new walker : codex.wordpress.org/Class_Reference/Walker
    – mmm
    Oct 18 '17 at 9:43
  • I don't really understand. What would I need the filters for? It already returns all the top level menu items from my selected menu. A filter isn't going to add submenu items to the response of the function is it? I just want an array of my menu and submenu... Surely there is a simple way to achieve this and other people have come across this before?
    – Stretch0
    Oct 18 '17 at 9:51
  • If you're trying to use Bootstrap for your menu, this is a solved problem, use the Bootstap Nav Walker: github.com/wp-bootstrap/wp-bootstrap-navwalker Oct 18 '17 at 10:06
1

How to get a multidimensional array of my menu?

https://stackoverflow.com/questions/11935423/how-do-i-generate-a-custom-menu-sub-menu-system-using-wp-get-nav-menu-items-in-w

1
  • Please include at least summary of the information, if you are linking elsewhere. Links die over time.
    – Rarst
    Oct 18 '17 at 11:11
0

Hierarchical array isn't something WP can provide out of the box, since that's not how it processes menus.

Generation of menu markup is done by a Walker class, which takes care of turning flat input into a resulting markup, with levels and everything.

So for custom menu markup it is a typical "WordPress way" to use a custom walker rather than write a completely custom routine with custom structure inputs.

Given Bootstrap popularity specifically, I am pretty sure you won't have much trouble finding ready-made Walker examples out there for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.