如果要返回一个项目数组(而不是像wp_nav_menu
), 您可以尝试以下操作:
$menu_name = \'your_menu_location\';
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 ) {
if ($menu_item->menu_item_parent != 0 ) continue;
$title = $menu_item->title;
$url = $menu_item->url;
DO WHAT EVER YOU WANT HERE
}
} // endif has nav menu or not