How To Disable Fallback Menu In WordPress?

In WordPress, when you register the menu for your site and display it, then, if you do not assign the menu to the required theme location then, it will display the list of pages added within your site. But, what if you do not like this feature provided by WordPress at all and you want to disable it? Is this really doable? Well it is doable and here we will learn about how to disable fallback menu in WordPress. So, let’s move ahead and learn it, shall we?

How to register menu in WordPress?

First of all, you might need to know about how to register the menu in WordPress themes. So, let’s first lean about it.

For registering the menu in WordPress, you need to hook the function inside the after_setup_theme hook, which contains the below PHP code inside it:

register_nav_menus( 
array(
'primary' => esc_html__( 'Primary', 'themetextdomain' ),
)
);

So, the final function to register the menu for your theme is as below:

function theme_prefix_setup_theme() {
register_nav_menus(
array(
'primary' => esc_html__( 'Primary', 'themetextdomain' ),
)
);
}
add_action( 'after_setup_theme', 'theme_prefix_setup_theme' );

How to display the registered menu in your WordPress site?

Now, since you know about how to register the menu for your site/theme, now let’s move ahead and learn to display the registered menu in WordPress, shall we?

In order to display the registered menu within your theme, you need to add the below PHP code where ever you need to display the menu:

wp_nav_menu(
array(
'theme_location' => 'primary'
)
);

Now, since adding the above PHP code only, it by default have the fallback set to display the pages added in your site. So, in order to disable fallback menu in WordPress, we need to modify this array as needed.

So, let’s move ahead and learn about how to achieve it within your theme, shall we?

How to disable fallback menu in WordPress for your theme?

As from previous code, you have learn’t that, you can alter the wp_nav_menu function as needed to disable fallback menu in WordPress for your theme. So, let’s alter the arguments for mentioned function as needed, which is as below:

wp_nav_menu(
array(
'theme_location' => 'primary',
'fallback_cb' => false, // Disables the fallback menu, which displays the pages added within your site.
)
);

After displaying the menu in your site via above PHP code, you can see that it will no longer display the pages added in your site.

You can see in above screenshot, we have added he respective comments as needed and its respective output. Also, the other way to achieve the same result is via the below PHP code where ever you display it within your site:

if ( has_nav_menu( 'primary' ) ) {
wp_nav_menu(
array(
'theme_location' => 'primary',
)
);
}

Conclusion

If you have followed this tutorial properly and can now easily disable fallback menu in WordPress theme then, we can assume that you are more familiar on this than before. However, if you are still confused on this and want to know more then do drop the comment below on the comment box and we will follow it up.

You may also want to read:

  1. Fix White Screen Of Death In WordPress
  2. Disable Gutenberg Editor And Keep Classic Editor In WordPress
  3. Add Theme Support For JetPack Infinite Scroll
  4. How To Create Shortcode With Attributes In WordPress?
  5. Integrate Bootstrap NavBar 4 Into WordPress Theme
  6. Display Image Caption Under Featured Images In WordPress

Post navigation

Bishal Napit

Bishal Napit is a WordPress theme developer from Tansen, Palpa, with a passion to learn more on WordPress.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.