0

I'm trying to get a menu items created from wordpress admin panel.

To do this I followed the instructions codex: http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items

// Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
    // This code based on wp_nav_menu's code to get Menu ID from menu slug

    $menu_name = 'custom_menu_slug';

    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;

But I get always the error: Menu "cs-1-language" not defined.

I tried with the name and the slug and different menus but I always find that error.

Does anyone know what I'm doing wrong or if there is another way to list the items of a menu?

Thanks!

0

I tried with the name and the slug and different menus but I always find that error.

Did you try with location slug? Take a look at where your menu location is defined (functions.php), for example:

register_nav_menus( array(
    'primary' => __( 'Primary Menu', 'mytheme' ),
) );

On Appearance->Menus assign your menu to that location.

And then run your code starting with:

$menu_name = 'primary';
0

Thanks for your help

Did you try with location slug? Take a look at where your menu location is defined (functions.php)

Yes, I was use this function to convert menu name to slug but same results...

function wp_menu_id_by_name( $name ) {
$menus = get_terms( 'nav_menu' ); 

foreach ( $menus as $menu ) {
    if( $name === $menu->name ) {
        return $menu->slug; 
    }
}

return false;

}

I created the menus in menu panel(apperance->menus), not in functions.php

I try create in functions.php but not appear the menus in menu panel :(

  • You should file an edit to your question when you have additional details to add. – Gabriel Feb 23 '15 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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