menu item in array

给你一囗甜甜゛ 提交于 2019-12-29 09:27:12

问题


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?


回答1:


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;
    }
    }



回答2:


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/



来源:https://stackoverflow.com/questions/11644583/menu-item-in-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!