WordPress – How To Register Menu And Show in Theme

wordpress-register-menu
In this WordPress tutorial, I will explain how to:

  • Register menu
  • Display menu in your theme.

Register menu

Let’s say, we want to register / create two menus. One for header and another for footer.
Copy paste below code in functions.php file of your theme.

1
2
3
4
5
6
7
8
9
10
11
12
/**
* Register menus
*/

function register_my_menus() {
 register_nav_menus(
   array(
     'header-menu' => __( 'Header Menu' ),
     'footer-menu' => __( 'Footer Menu' )
    )
  );
}
add_action( 'init', 'register_my_menus' );

Above code will register two menus.
1) Header Menu
2) Footer Menu

wordpress-menu

Show menu in theme

To show header-menu in your WordPress theme, add following codes in template file where you wish to show menu like in header.php

1
2
3
<nav class="header-menu">
  <?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>
</nav>

Similarly, to show footer-menu, use below code.

1
2
3
<nav class="footer-menu">
  <?php wp_nav_menu( array( 'theme_location' => 'footer-menu' ) ); ?>
</nav>

Hope this article was helpful to you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *