• Resolved gotsanity

    (@gotsanity)


    I am attempting to use the little snippet of code on the codex page for wp_get_nav_menu_items() but every time I attempt to run the code with the menus on my test site it always comes up “menu-blah not defined”

    Code:

    $menu_name = 'short-menu';
    
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    		$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    
    		$menu_items = wp_get_nav_menu_items($menu->term_id);
    
    		$menu_list = '<ul id="menu-' . $menu_name . '">';
    
    		foreach ( (array) $menu_items as $key => $menu_item ) {
    			$title = $menu_item->title;
    			$url = $menu_item->url;
    			$menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
    	}
    		$menu_list .= '</ul>';
        } else {
    		$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
        }
        // $menu_list now ready to output
    	echo $menu_list;
Viewing 6 replies - 1 through 6 (of 6 total)
  • Try this

    $menu_name = 'short-menu';
    
    if ( ( $locations = get_nav_menu_locations($menu_name) ) && isset( $locations[ $menu_name ] ) ) {

    In the documentation the get_nav_menu_locations($menuID) states that the first parameter “Menu ID” is required, which is funny because the code example that is listed doesn’t have it included. See if that fixes it.

    Thread Starter gotsanity

    (@gotsanity)

    no, that didn’t seem to work either. still get the same error. i know the menu is defined because later on in the code i have it output just fine with the normal wp_nav_menu() as a method of debugging. i would just use that method to output but I need to strip the css classes from the menu in order to display its details independently of user generated themes so that they don’t muddle up the jquery effects I am using

    I was able to get the menu to output by skipping the locations function. I created a menu from the admin side named the same as yours and was able to get the unordered list printed out. Hopefully it works for you

    $menu_name = 'short-menu';
    
    if ( ($menu = wp_get_nav_menu_object( $menu_name ) ) && ( isset($menu) ) ) {
    	$menu_items = wp_get_nav_menu_items($menu->term_id);
    	$menu_list = '<ul id="menu-' . $menu_name . '">';
    
    	foreach ( (array) $menu_items as $key => $menu_item ) {
    		$title = $menu_item->title;
    		$url = $menu_item->url;
    		$menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
    	}
    
    	$menu_list .= '</ul>';
    
    } else {
    
    	$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
    }

    Thread Starter gotsanity

    (@gotsanity)

    ricoh, you sir, are a gentleman and a scholar. It worked as expected. Now just to debug some css inheritance issue and I should be able to get a working model running shortly. Thanks for the help.

    Hi guys, got the same issue, and Ricoh’s code resolved my issue too.

    But why is the $location in the codex example http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items
    preventing it from running as we tweaked it here?

    Trying to understand why.

    Grz, ssstofff

    Moderator bcworkz

    (@bcworkz)

    I can’t replicate the error, so I’m speculating. The $location portion gets the menu ID from where the theme is supposed to store it. If the theme does not store menu IDs in the way expected, the locations function will return an empty or malformed array, causing the rest of the chain of functions to fail.

    As it happens, we do not need the menu ID to get the menu object, we can use the name or slug as well. Since we have the menu name, there is no need to get the ID from the location, we can get the object directly by name. The example is based on code where only the location was known, not the name or slug, thus in that case the location step was necessary.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp_get_nav_menu_items() showing not defined with all menu slugs’ is closed to new replies.