WordPress.org

Ready to get started?Download WordPress

Forums

register_nav_menu() issue (7 posts)

  1. c.bavota
    Member
    Posted 4 years ago #

    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 the theme_location argument. It works perfectly if both menu are created and assigned to each registered menu location.

    If there are no menus, the fallback_cb argument 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.php file 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_cb

    If a menu has been registered and no menu has been assigned to it, it seems like it should default to the fallback_cb as opposed to get the first menu that has items.

  2. c.bavota
    Member
    Posted 4 years ago #

    I messed around with nav-menu-template.php and 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;
    			}
    		}
    	}
  3. Ryan Boren
    WordPress Dev
    Posted 4 years ago #

    Looking into this for ticket 13378.

  4. Ryan Boren
    WordPress Dev
    Posted 4 years ago #

    Thanks for the patch. Committed.

  5. c.bavota
    Member
    Posted 4 years ago #

    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.

  6. Mr Papa
    Member
    Posted 4 years ago #

    http://core.trac.wordpress.org/

    submit tickets there...

  7. silvergrad
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic