menu item in array

2020-02-15 05:07发布

How do I get the menu items (depth = 1) into an array?

wp_nav_menu outputs a formatted list with both ul and li elements. wp_list_pages also outputs a formatted list with both ul and li.

I just want to get the menu items (striped of tags) of depth one into an array.

How do I accomplish this?

2条回答
手持菜刀,她持情操
2楼-- · 2020-02-15 05:34

Thanks @Libin

Figured out wp_get_nav_menu_items()

$menu_name = 'sidebar-menu'; //menu slug
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

echo "<pre>";
print_r($menuitems);
echo "</pre>";

here i am getting the whole menu items

I fount one example here http://wiki.workassis.com/wordpress-get-menu-array/

查看更多
相关推荐>>
3楼-- · 2020-02-15 05:40

I think this will helps you: wp get nav menu items

    $menu_name = 'custom_menu_slug'; // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_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);


    foreach ( (array) $menu_items as $key => $menu_item ) {
        $title = $menu_item->title;
    }
    }
查看更多
登录 后发表回答