好吧,写这篇文章时,我真的很愚蠢,我在考虑首先循环菜单项,然后将它们存储在一个数组中以供以后使用。通过以下页面上的示例,我可以做到:
http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items
// 基于$menu\\u name获取导航菜单(与wp\\u nav\\u menu的“theme\\u location”或“menu”arg相同)//此代码基于wp\\u nav\\u menu的代码从menu slug获取菜单ID
$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
上面的代码只需要进行一些调整,但可以做到!:)