wp_nav_menu() doesn't work

I'm working on a theme with custom menus, currently using WP 4.0.1.

I'm registering two menus in functions.php:

register_nav_menus(
    array(
      'primary'   = __( 'Main Menu', 'wsy' ),
      'secondary' = __( 'Secondary Menu', 'wsy' )
    )
);

Then, displaying them in my header.php file:

nav role="navigation"
    ?php
        wp_nav_menu(
            array(
                'theme_location' = 'primary',
                'depth'          = '1'
            )
        );
    ?
/nav

The problem is, when I select a specific menu from either the Menus page or the customizer, they stop showing up in my page. It doesn't matter which menu or which location, they just don't show up. When I reset the locations (choose "- Select -" from the dropdown) a default menu is shown.

WP_DEBUG is active, no errors. Tried it with WP 4.1, still nothing. Tried adding new menus, no luck. Tried with a single menu location and registering with register_nav_menu(); instead -- nothing works.

I even tried on a fresh WP install, no luck. Also tried without the depth parameter, nothing.

I'd appreciate any help with this. Thanks!

Topic navigation menus Wordpress

Category Web


You might need to use 'add_theme_support' for 'nav-menus' as well. See line 2 below:

if ( function_exists('wp_nav_menu') ) {
  add_theme_support( 'nav-menus' );
  register_nav_menus( array( 
      'primary' => __( 'Main Menu', 'wsy' ), 
      'secondary' => __( 'Secondary Menu', 'wsy' )
      ) 
  );
}

Then display your menu like so:

$nav_menu = 'primary';
if ( has_nav_menu( $nav_menu ) ) {
echo '<nav role="navigation">';
  wp_nav_menu(
    array(
        'theme_location' => $nav_menu,
        'depth' => 1
    )
  );
echo '</nav>';
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.