正如@vancoder提到的,wp_get_nav_menu_items()
不过,我认为更详细的答案对未来的人们更有帮助。
所述函数返回WP_Post Object
对象(因此可以使用箭头访问值,例如。$item->title
).
对于基本设置,可以使用以下选项:
$menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php)
// This returns an array of menu locations ([LOCATION_NAME] = MENU_ID);
$menuID = $menuLocations[\'primary\']; // Get the *primary* menu ID
$primaryNav = wp_get_nav_menu_items($menuID); // Get the array of wp objects, the nav items for our queried location.
然后你就可以绕过去
$primaryNav
变量,例如:
foreach ( $primaryNav as $navItem ) {
echo \'<li><a href="\'.$navItem->url.\'" title="\'.$navItem->title.\'">\'.$navItem->title.\'</a></li>\';
}