如果您知道菜单的slug,那么事情就简单多了,否则您可以使用此函数在指定位置获取菜单。
<?php
function wpse45700_get_menu_by_location( $location ) {
if( empty($location) ) return false;
$locations = get_nav_menu_locations();
if( ! isset( $locations[$location] ) ) return false;
$menu_obj = get_term( $locations[$location], \'nav_menu\' );
return $menu_obj;
}
?>
那么
//if you after the menu the menu with a specific ID / Slug
//$menu_obj =wp_get_nav_menu_object($id_slug_or_name);
//if you after the menu at a specific location
$menu_obj = wpse45700_get_menu_by_location($location);
echo "<h3>".esc_html($menu_obj->name)."</h3>";
//Display menu here
或者,您可以将其作为中items属性的参数的一部分传递,而不是回显html
wp_nav_menu
.
例如,要在“primary”位置显示菜单,请执行以下操作:
$location = \'primary\';
$menu_obj = wpse45700_get_menu_by_location($location );
wp_nav_menu( array(\'theme_location\' => $location, \'items_wrap\'=> \'<h3>\'.esc_html($menu_obj->name).\'</h3><ul id=\\"%1$s\\" class=\\"%2$s\\">%3$s</ul>\') );