多么卑鄙,没人回应。我费了好大劲才先检查了walker的课程,才发现wp_get_nav_menu_items()
这就是我想到的。虽然不完美,但它可以工作(只有两个级别,对我的页面来说足够了)。
$menu_name = \'primary\';
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;
$url = $menu_item->url;
$curr_item_parent = $menu_item->menu_item_parent;
$curr_item_id = $menu_item->db_id;
if($curr_item_parent)
{
$sub_list[$curr_item_parent][] = array(\'title\' => $title, \'url\' => $url, \'id\' =>$curr_item_id, \'parent_id\' => $curr_item_parent);
$parent_list[] = $curr_item_parent;
}
else
{
$tab_list[] = array(\'title\' => $title, \'url\' => $url, \'id\' =>$curr_item_id);
$toplevel_list[] = $curr_item_id;
}
$complete_list[] = $curr_item_id;
}
}
$parentless_list = array_diff ($toplevel_list, array_unique($parent_list));
var_dump($parentless_list);
$menu_output .="<ul class=\'nav nav-tabs\'>";
foreach($tab_list as $tab)
{
$tab = (object)$tab;
$menu_output .="<li class=\'\'><a href=\'#{$tab->id}\' data-toggle=\'tab\'>{$tab->title}</a></li>";
}
$menu_output .= "</ul>";
$menu_output .= "<div class=\'tab-content\'>";
foreach($sub_list as $key=>$sub_array)
{
$menu_output .= "<div class=\'tab-pane\' id=\'{$key}\'>";
foreach($sub_array as $sub)
{
$sub = (object)$sub;
$menu_output .="<li class=\'\'><a href=\'#{$sub->url}\' data-toggle=\'tab\'>{$sub->title}</a></li>";
}
$menu_output .= "</div>";
foreach($parentless_list as $dummy_item)
{
$menu_output .= "<div class=\'tab-pane\' id=\'{$dummy_item}\'></div>";
}
}
$menu_output .= "</div>";
echo $menu_output;