0

Can someone tell me how the wp_nav_menu functions when no menu or theme_location is specified?

I have multiple sites that use an interior navigation setup that does not specify the menu. It would seem that this results in WordPress searching within all menus until the page is found. For 99% of my sites, this functionality works perfectly.

However, I've noticed if multiple menus contain the same page (and these pages have different children specified to them), wp_nav_menu seems to return the first menu that it discovers which contains the desired page. In certain instances, this is ends up being from a menu that does not contain all of the pages in main navigational hierarchy.

Therefore, I was wondering if there was a way to specify the menu order (i.e. check the main menu first, then the footer, then utility, etc), or if a master menu should be implemented that is never displayed on the site, but is utilized to provide the proper page hierarchy for the site.

Any thoughts on approaches or best practices would be helpful.

For full disclosure, please note that I have extended my wp_nav_menu by utilizing WP Nav Plus, but I do not think that plugin changes my question

3
  • easiest thing to do is look in source
    – Milo
    Commented Dec 11, 2014 at 18:06
  • $menus = wp_get_nav_menus( array( 'orderby' => 'name' ) ); does clarify how it works. Am I correct in assuming that if it isn't found in the first non-empty menu, it stops attempting to look in other menus?
    – Eric K
    Commented Dec 11, 2014 at 22:43
  • if there's no menu or theme location, there's nothing to look for in other menus- any valid menu is as likely to be the correct one as any other valid menu, so there's no reason to continue looking.
    – Milo
    Commented Dec 11, 2014 at 23:00

1 Answer 1

1

I am not confident I am following your description completely, but verbatim from the documentation:

If not given a theme_location parameter, the function displays

  • the menu matching the ID, slug, or name given by the menu parameter;
  • otherwise, the first non-empty menu;
  • otherwise (or if the menu given by menu is empty), output of the function given by the fallback_cb parameter (wp_page_menu(), by default);
  • otherwise nothing.

Codex > Function Reference/wp nav menu

The behaviour you are seeing might just be incidental to the what menu is “first”, not following some complicated page–related logic.

As for recommendation — why rely on on something like that in first place? Just make it explicit which menu you need and eliminate any ambiguity.

1
  • I'm not sure making it explicit (other than through the use of a master menu) is possible. In my layout, I have a main menu, utility (or secondary) menu, footer menu, and utility menu in the footer. These are all different, but they all link to pages. It doesn't seem to make much sense to have different interior navs functions depending on what menu the user clicked on.
    – Eric K
    Commented Dec 11, 2014 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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