Skip to main content

WordPress – How To Register Menu And Show in Theme

Submitted by admin on Thu, 12/09/2021 - 09:23
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.


/**
* 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 following 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


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

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


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

Hope this article was helpful to you.

Category

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.