0

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']);

1 Answer 1

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=' );
1
  • 1
    Excellent. This answers the question, thank you
    – Valeron
    Nov 24, 2020 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.