register_nav_menu() issue
-
I am testing out the nav menu function in 3.0-beta2-14769 and it is pretty cool. I came across something that might be a bug and I am not too sure how I am suppose to go about it so I am posted it here. (Please let me know if I am in the wrong place.)
I have registered two menus in my theme using the following code:
register_nav_menu('main', 'Main Navigation Menu'); register_nav_menu('sub', 'Sub-Navitation Menu');When calling the registered menu within the
wp_nav_menu()function, I use thetheme_locationargument. It works perfectly if both menu are created and assigned to each registered menu location.If there are no menus, the
fallback_cbargument allows me to create a function to set a default state, which is great.The problem occurs if no menu is assigned to a specific registered menu location. If only one menu has been created, both locations display it whether it was assigned to that location or not.
I looked at the
nav-menu-template.phpfile and it seems that the process for getting the nav menu works like this:1. Get the nav menu based on the requested menu name 2. If no menu, get the nav menu based on the theme_location 3. Get the first menu that has items if we still can't find a menu 4. If a menu exists, get its items 5. If no menu was found or if the menu has no items, call the fallback_cbIf a menu has been registered and no menu has been assigned to it, it seems like it should default to the
fallback_cbas opposed toget the first menu that has items.
-
I messed around with
nav-menu-template.phpand figured out a super simple solution, but I am not sure if it now works the way you guys intended.Line 208 in nav-menu-template.php // get the first menu that has items if we still can't find a menu if ( ! $menu ) { $menus = wp_get_nav_menus(); foreach ( $menus as $menu_maybe ) { if ( $menu_items = wp_get_nav_menu_items($menu_maybe->term_id) ) { $menu = $menu_maybe; break; } } } Change it to: // get the first menu that has items if we still can't find a menu if ( ! $menu && !$args->theme_location) { $menus = wp_get_nav_menus(); foreach ( $menus as $menu_maybe ) { if ( $menu_items = wp_get_nav_menu_items($menu_maybe->term_id) ) { $menu = $menu_maybe; break; } } }Looking into this for ticket 13378.
Thanks for the patch. Committed.
Awesome! Is there a better way to go about submitting bugs I find? I am going to be working with the latest beta version to update all of my premium themes and I might encounter some other issues.
submit tickets there…
If I may ask, how DID you use wp_nav_menu? I have code like this:
register_nav_menus( 'main_menu' , __( 'Main Menu', 'silvergrad' )); register_nav_menus( 'sub_menu' , __( 'Sub Menu', 'silvergrad' ));and:
wp_nav_menu('theme_location=sub_menu&container=&menu_class=menu_sub');That unfortunately doesnt work, it uses the callback function. Any ideas? I know that the theme_locations have been set properly.
Thanks!
EDIT: Never min, I figured it out. Was using old
register_nav_menu<strong>s</strong>()code.
The topic ‘register_nav_menu() issue’ is closed to new replies.