我已将菜单名称保存为'main_nav',并尝试获取其中的所有项目:
$menu_name = 'main_nav';
$locations = get_nav_menu_locations();
var_dump($locations[$menu_name]); // NULL
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 = '<nav>' ."\n";
$menu_list .= "\t\t\t\t". '<ul>' ."\n";
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "\t\t\t\t\t". '<li><a href="'. $url .'">'. $title .'</a></li>' ."\n";
}
}
但我null
获得了$locations[$menu_name]
。我错过了什么想法?
答案 0 :(得分:1)
当$menu_name
尝试在powers.php文件中应用register_nav_menus
函数中使用的键时。