Get menu items problem

admin2025-01-08  6

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

To do this I followed the instructions codex:

// 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!

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!

Share Improve this question edited Feb 23, 2015 at 10:11 David Gard 3,3005 gold badges26 silver badges38 bronze badges asked Feb 23, 2015 at 9:10 Serg10Serg10 11 bronze badge
Add a comment  | 

3 Answers 3

Reset to default 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';

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

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>

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269762a1376.html

最新回复(0)