2

I'm a novice WordPress developer and I'm looking for best way to build custom WordPress menu.

I have found two server side ways to do that, but i don't understand deeper difference of them

The menu might build with the wp_nav_menu function and for that need crate extends class of Walker_Nav_Menu. For that need have a copy of the main class in your theme and change more code as well

Or we can use wp_get_nav_menu_items function, by giving it the menu location to get the array of all items to build html output. It will be more simple and easy and no need to understand complicated Walker class

So, what the best way to build the menu or what the main difference of these functions.

Thanks

  • the function wp_nav_menu use the result of wp_get_nav_menu_items. wp_nav_menu is the recommanded way of generating the menu in a theme in order that plugins can customise it with filters and actions. do you develop a plugin or a theme ? – mmm Jul 9 '18 at 11:20
  • Thank you mmm for quick answer, I have been developing theme and probably need using wp walker class. – DmitriyRF Jul 9 '18 at 13:44
  • Why do you think that you need to create your own Walker class in your theme? – Krzysiek Dróżdż Jul 9 '18 at 14:16
  • I'm using Bootstrap for my theme, and be good to have native build in framework menu – DmitriyRF Jul 11 '18 at 8:55
1

wp_get_nav_menu_items function retrieves an array of menu items for given menu. But the menu is a hierarchy, so there is a need of creating some mechanism that will create that hierarchy based on list of menu items.

On the other hand there already is wp_nav_menu, that will get items for that menu (using wp_get_nav_menu_items) and then it will pass it to Walker class that will generate HTML code for this menu. This function will also take care of cases when given menu doesn't exist and so on. It is the recommended way to display menu in themes.

PS. I don't get why you think that you "have to" write your own Walker class. You certainly don't. WP will use it's own Walker class as default. And if you want to customize the HTML code for that menu, you can write filters and it will be enough most of the times...

  • I'm using Bootstrap for my theme, and be good to have native build in framework menu – DmitriyRF Jul 11 '18 at 8:56
  • @DmitriyRF Try this, no need to reinvent the wheel – brianjohnhanna Jul 11 '18 at 14:35

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.