Navigation-Menus are important parts of your theme Since it allows visitors to navigate quickly and allows them to jump on a particular section, page, category, or post.
Just like Sidebars, before displaying Navigation menus in our theme we first have to register those in functions.php
the file of our WordPress theme.
So to define our own custom navigation menu in the theme, we have to:
- First Register Navigation Menu in
function.php
file of our theme. - Display the Navigation menu on desired location by editing template file.
Regsiter Menus
Here is the process that you need to follow to register navigation menu:
- Add following code in
function.php
:function register_my_menus(){ register_nav_menus( array( 'header-menu' => __('Header Menu'), 'footer-menu' => __('Footer Menu') ); ) } add_action('init', 'register_my_menus');
- Save the file.
Here we successfully registered two navigation menus, one to show in the header and one for display in the footer of our theme.
The next thing we need to do is edit header.php
file to add Header Menu and footer.php
to add Footer Menu.
Display Menus
To Display the Navigation menu on the specific location, we use wp_nav_menu()
.
Here is the process:
- Edit your
header.php
file. Add specifiec parameter to customize menu. - Our Simple Navigation menu code would look something like this:
wp_nav_menu( array( 'theme_location' => 'header-menu' ) );
- Do the same thing with the
footer.php
file for the Footer Menu:wp_nav_menu( array( 'theme_location' => 'footer-menu' ) );
You can learn more about wp_nav_menu()
on official WordPress documentation – https://developer.wordpress.org/reference/functions/wp_nav_menu/.