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!

3 Answers 3

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 :(

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

Here is another way to get the items of a menu.

If you are in wordpress backend editing your menu, if you hover the 'delete menu' link, you will see in id of your menu in the url. You can get the menu by id:

<?php $menuID = '12'; // ID of your menu $primaryNav = wp_get_nav_menu_items($menuID); // get your menu items ?>

So you get all the items in the menu. You can then loop through them with something like:

<ul> <?php foreach( $primaryNav as $item ) { $link = $item->url; // get the url of item $title = $item->title; // get title of item ?> <li class="item"><a href="<?php echo $link; ?>"><?php echo $title; ?></a></li> <?php } ?> </ul>

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.