使用has_nav_menu
检查位置是否已分配菜单
<?php
if ( has_nav_menu( $location ) ) {
echo "Also in this section";
}
?>
http://codex.wordpress.org/Function_Reference/has_nav_menu
有关更高级的检查,您需要致电
wp_get_nav_menu_items
并处理返回的项目,查看其中是否有子项。
下面是一个输出菜单的示例,显示了如何使用其输出:
// Get the nav menu based on $menu_name (same as \'theme_location\' or \'menu\' arg to wp_nav_menu)
// This code based on wp_nav_menu\'s code to get Menu ID from menu slug
$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
需要注意的一点是,导航菜单由导航菜单项组成,
and these menu items are custom post types. 使用父子关联来确定深度,就像处理页面和帖子一样