Setting a Fallback for WordPress Menu

WordPress 3 has been around for quite some time now, but there are still many people who are not familiar with the new WordPress custom menu admin. As a theme developer, it is essential to provide a fallback for your menu so that your buyers/theme users do not think that something is wrong with your theme. In this article, we will show you how to create a custom fallback with a link to “wp-admin/nav-menus.php” so that users can set up their menu right away after installing the theme.

Step 1: Create The Fallback Function

The first thing you need to do is add a new function to your functions.php file (or any other file you use to register your custom menu areas). Add the following code, preferably right after the register_nav_menu function for better usability. This function will grab the new file that you will create in step 2 with the code for your default/fallback menu.

// Menu Fallback

function wpex_default_menu() {

get_template_part( ‘template-parts/default-menu.php’ );

}

Step 2: Set Up Your Default/Fallback Menu

Now you need to create the file that the function added previously will grab and display when a menu isn’t defined in the WP admin for your menu area.

1. Create a new folder called “template-parts” in your theme folder if one doesn’t exist already.

2. Create a new file called default-menu.php.

3. Insert your code for your fallback menu in this file (make sure it matches the code used on your site so it looks nice). Below is a basic example:

Stay in Touch

spot_img

Related Articles