admin管理员组

文章数量:1314480

Why do my created Menus on the WordPress Admin dashboard appear in customtemplate.php DESPITE not defining it the correct 'theme_location'?

It will show in that location EVEN if the created menu in the WordPress Admin dashboard is not set to show in any location.

It only hides if the following if check is tested. Why?

if(has_nav_menu('menu-ID1'))

Functions.php

register_nav_menus(
            array(
                'menu-ID1' => __( 'MenuName', 'blabla' ),
            )
        );

customtemplate.php

//Still displays even though the registered menu-ID1 is not used    
wp_nav_menu(['theme_location' => 'randomname']);

Why do my created Menus on the WordPress Admin dashboard appear in customtemplate.php DESPITE not defining it the correct 'theme_location'?

It will show in that location EVEN if the created menu in the WordPress Admin dashboard is not set to show in any location.

It only hides if the following if check is tested. Why?

if(has_nav_menu('menu-ID1'))

Functions.php

register_nav_menus(
            array(
                'menu-ID1' => __( 'MenuName', 'blabla' ),
            )
        );

customtemplate.php

//Still displays even though the registered menu-ID1 is not used    
wp_nav_menu(['theme_location' => 'randomname']);
Share Improve this question edited Nov 24, 2020 at 6:56 Valeron asked Nov 24, 2020 at 2:55 ValeronValeron 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I'm guessing your menu contains only Pages (posts of the page type) on your site?

But nonetheless, with wp_nav_menu(), if you specify a theme_location that doesn't exist or contains no menu (i.e. no menus assigned to that location), then the fallback_cb arg (which defaults to wp_page_menu()) will determine what is displayed.

So if you want the function to display the menu only if it's assigned to the specified theme location, you can do the has_nav_menu() check:

if ( has_nav_menu( 'menu-ID1' ) ) {
    wp_nav_menu( 'theme_location=menu-ID1' );
}

or set the fallback_cb to an empty string:

wp_nav_menu( array(
    'theme_location' => 'menu-ID1',
    'fallback_cb'    => '',
) );

// or you may pass the function args as a query string:
wp_nav_menu( 'theme_location=menu-ID1&fallback_cb=' );

本文标签: navigationNav Menu Theme Location not working