2

Are there any benefits to using a nav walker class over iterating through wp_get_nav_menu_items()?

Nav walkers seem very clunky to me and iterating over an array feels easier and more re-usable but it then bypasses the internal menu filters of wordpress that I don't know much about. Will this make my theme incompatible with some plugins?

Best practice advice here would be greatly appreciated.

  • it have no benefits at all, it is just adding endless learning material in the development. Such a times waster. – KeepMove Sep 2 '15 at 4:41
1

I would say mostly you see the usage of wp_nav_menu(), which does use wp_get_nav_menu_items(). You can use wp_nav_menu() with the default walker or a custom walker.

To be honest I can't remember the time I did a menu manually, with wp_get_nav_menu_items() or something else, and I tend to be lazy, so this is definitely more work.

The Walker Class gives you a lot of possibilities as there is more information available, which definitely is big plus. So whether you should leverage a walker - or otherwise (I'd say) use wp_nav_menu() - is pretty much a question of what you need to achieve, for more complex menus I personally would always go with a walker. Last but not least, walkers are nicely reusable, which is pretty neat and another plus.

  • Is the only way to set classes for <li> in the navwalker? on start_el() the $item->classes arg has some classes added by wordpress. Would I have to use a filter to change those without using the nav walker? – Guerrilla Apr 23 '15 at 20:57
  • Actually it seems I can't even edit <li> with the navwalker so I think the other route is my only option – Guerrilla Apr 23 '15 at 21:06
  • You are able to change the class via the nav_menu_css_class hook. For the id use the nav_menu_item_id hook. @Guerrilla – Nicolai Apr 23 '15 at 21:30
  • looks like that hook doesn't pass item depth which I think makes it non-viable for me as the templates I get to convert have list item classes that change at varying depths. – Guerrilla Apr 23 '15 at 21:35
  • The depth is passed, always look at the source. @Guerrilla – Nicolai Apr 23 '15 at 21:40
0

Use the default walker, it covers 99.999999999999.... ... 999999999999% of instances via its filters, and it's maintained by someone smarter than both of us, and used by a lot of people.

If you need to do something that requires modifying the walker, use a subclass.

Otherwise you're going to lose compatibility with anything that uses the walker filters, and half the features from the nav menu interface will need to be reimplemented from scratch

  • I there a way to edit <li> with navwalker? I am looking at the start_el() and it just does the link. It does not add the <li> tag. – Guerrilla Apr 23 '15 at 21:07
  • It sounds like you're trying to do something but you've asked this instead of asking about your problem, you'll have to be more specific about what you mean by edit – Tom J Nowell Apr 24 '15 at 1:15
  • I didn't know that the walker didn't "edit" <li> before I asked the question. What I mean by "edit" is to add my own classes and attributes to the tag. Ialocin pointed out I could use a filter for this so that's the route I'll take. My original question was asking if there were any benefits of iterating yourself. My follow up question about <li> directly relates to this as if you can't edit certain things then that would be a benefit/disadvantage. – Guerrilla Apr 24 '15 at 16:20
0

Flexibility & cleanliness.

Walker-based classes are an excellent way to generate custom output based on any branched data object. They keep your code lean, mean, and easily readable. You can define output for the start and end of each "branch" (any menu item with sub-menu items) as well as the start and/or end of each individual node/menu item. It's my go-to for generating complex custom menu markup using WordPress's menu objects.

  1. First, create your custom walker class.
  2. Then use wp_nav_menu() in your theme, and define 'walker' => new YourWalker as one of the arguments.
  3. That's it.

See more about the Walker class's usage here.

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.