Declaring + Using Custom WordPress Menus
With a quick little WordPress drop-in function, you can declare various menu locations within your theme. Most commonly, you may wish to set a main menu and possibly a footer menu for supplementary navigation items. I will typically also declare a mobile menu location, just in case my client wants to use a different menu for that area (ex: a larger, consolidated version of the two separate menus — main menu and footer menu — on desktop.)
I’ve previously blogged about keeping a well-organized functions.php file so following that same pattern, I would might create a nav-menus.php file within my functions folder and drop in:
<?php // add specific WP menus register_nav_menus( array( 'main_menu' => 'Main Menu', 'footer_menu' => 'Footer Menu', 'mobile_menu' => 'Mobile Menu' ) );
Including this file into my theme’s functions.php file will create the various locations which will now appear under Appearance > Menus in the WordPress admin to allow specific menus be assigned there. When it comes to pulling a specific menu into the theme, I would then reference the menu location like:
<?php wp_nav_menu( array('theme_location' => 'main_menu')); ?>
Simple to do to create very easily assignable menu locations within your theme!