admin管理员组

文章数量:1122846

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!

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!

Share Improve this question asked Mar 25, 2015 at 5:30 WSYWSY 111 silver badge3 bronze badges 8
  • See this post on how to create a custom menu. Hope that helps :-) – Pieter Goosen Commented Mar 25, 2015 at 6:04
  • @PieterGoosen My code is written like that, but it's still not working. Thank you anyway Pieter :) – WSY Commented Mar 25, 2015 at 8:16
  • Test this on a freash install without plugins on one of the bundled themes like twentyfifteen. I suspect something in your theme is causing this issue. It is definitely not your code or the code in my linked answer. – Pieter Goosen Commented Mar 25, 2015 at 8:19
  • 1 Your problem is not the code you have posted. Are you using any custom filter on WP_Query or the main query, or any function hooked to pre_get_posts or any instance of query_posts. – Pieter Goosen Commented Mar 25, 2015 at 8:33
  • 1 @PieterGoosen You're right Pieter. I had a filter set on wp_nav_menu_items that was pulling in some extra items. I forgot I had commented the function responsible for returning those items... Wow, thank you so much for your help. I would have struggled a lot without your idea. – WSY Commented Mar 25, 2015 at 8:51
 |  Show 3 more comments

1 Answer 1

Reset to default 0

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>';
}

本文标签: menuswpnavmenu() doesn39t work