为什么我为$ locations [$ menu_name]获取null?

时间:2016-07-05 02:37:35

标签: wordpress wordpress-4.5.2

我已将菜单名称保存为'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]。我错过了什么想法?

1 个答案:

答案 0 :(得分:1)

$menu_name尝试在powers.php文件中应用register_nav_menus函数中使用的键时。