wp_nav_menu() fallback not receiving vars
-
I developed a custom walker class to implement the Bootstrap 3.0 menu structure so menus can be edited using the WordPress menu editor.
https://github.com/twittem/wp-bootstrap-navwalker
Everything works great except for the
$fallback_cbwhich is used by default when the theme is installed. I read in the codex that$fallback_cb
(string) (optional) If the menu doesn’t exist, the fallback function to use. Set to false for no fallback. Note: Passes $args to the custom function.The issue seems to be that the wp_page_menu function is not receiving the $args from the
wp_nav_menu()declaration.Here is my current
wp_nav_menu()declaration.<?php wp_nav_menu( array( 'menu' => 'primary', 'theme_location' => 'primary', 'depth' => 2, 'container' => false, 'menu_class' => 'nav navbar-nav', 'fallback_cb' => 'wp_page_menu', 'walker' => new wp_bootstrap_navwalker()) ); ?>Can anyone offer some insight into how I might resolve this issue?
-
The idea with the
fallback_cbis that you set up your own fallback function – which may (or may not) make use of wp_page_menu().I definitely understand that, is guess the question is
fallback_cbis undefined and defaults towp_page_menuis there any way to pass the definedwp_nav_menuvars over towp_page_menu?From what I recall, wp_nav_menu() uses slightly different parameters to wp_page_menu(). So I’d guess that there no easy way of simply passing them from one function to the other.
It is true that
wp_page_menu()uses different parameters, but any arguments you supply towp_nav_menu()will be passed to the ‘fallback_cb’ function. Whether your ‘fallback_cb’ function orwp_page_menu()can make use of these arguments is another story.The key line in
wp_nav_menu()source code is:
call_user_func( $args->fallback_cb, (array) $args );where
$argsis the merge of passed and default arguments, so you can see all arguments are passed to the ‘fallback_cb’ function. If this does not appear to be happening, it could be thewp_page_menu()does not know how to use a particular argument, but the argument is being passed none the less.Interesting. Looks like the
$argsare being passed to mywp_page_menu()function. After some further digging I notice that does not accept thecontainer&walkerparameters. Time to write my own.Thanks for sharing your knowledge!
The topic ‘wp_nav_menu() fallback not receiving vars’ is closed to new replies.