1

I have saved my menu name as 'main_nav', and I tried to get all the items in it:

$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";
    }
}

But I get null for $locations[$menu_name]. Any ideas what have i missed?

Rob
  • 14,746
  • 28
  • 47
  • 65
Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    and what do you get with `var_dump($locations)` ? ..also `($locations = get_nav_menu_locations())` is pretty crazy condition as you assign it 2 lines above (and you miss one `=`) – moped Jul 05 '16 at 02:54
  • i copied that from elsewhere but i guess it is a bad code. the menu name that i should pass it is 'primary' instead of 'main_nav' which I set it in the admin area. that is odd! – Run Jul 05 '16 at 03:08
  • 1
    well that code is actually fine, if you remove the 2nd line. in `if` condition what it does it tries to put output of function into $locations so if it's unsuccessful, it will return false - skips condition, if returns true/values, you'll check if `$locations[$menu_name]` is set .. – moped Jul 05 '16 at 03:13

1 Answers1

1

As the $menu_name try to apply the keys that were used in register_nav_menus function in your functions.php file.