How to register menu and call it in WordPress

Posted By :Ravi Rose |21st February 2019

Every theme has their own specific menu locations and menu support system.Navigation menus are the by default feature of WordPress themes.

Steps to add custom menu:-

1) First we have to add menu to our theme.

Adding Menu:-

To add a custom navigation menu to our theme first we need to register the newly created navigation menu.By adding the following code to your functions.php file of your theme's.

 

function wpb_custom_menu() {
  register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_menu' );

Now we have to check is our menu registered in our theme or not by performing the following steps i our WordPress Dashboard:-


1) Go to Appearance.
2) Click on Menus.
3) Now we have to create a new menu by clicking on the Menu item.

Now in this section we see a new menu created named as ‘My Custom Menu’.

And if we want to add more than one new navigation menus in our theme's, then we have to use the following code:

To add more than one custom menu we have to use an Array.

 

function wpb_custom_menu() {
  register_nav_menus(
    array(
      'my-custom-menu1' => __( 'My Custom Menu1' ),
      'my-custom-menu2' => __( 'My Custom Menu2' )
    )
  );
}
add_action( 'init', 'wpb_custom_menu' );

After adding our custom menus we have to display them in our WordPress theme.

 

To display the menu we have to use following code:-

 

<?php
wp_nav_menu( array( 
    'theme_location' => 'my-custom-menu', 
    'container_class' => 'custom-menu-class' ) ); 
?>

The most common place is to write it in header section but we can place it anywhere as per our requirement.

 

This is where we need to decide where we’d like to place our menu. If you’d like your menu to appear at the top of your page, you’ll need to edit the header.php file. You can also put it in your footer which means you would edit the footer.php file.

 

Now we can add CSS to our menu to make it more attractive as per our requirement.

Thanks


About Author

Ravi Rose

Ravi is a versatile Backend Developer with a strong expertise in WordPress technology. He is well-versed in the latest technologies like HTML, CSS, Bootstrap, JS, WordPress, PHP, and ReactJS. Ravi has contributed to multiple internal and client projects such as TripCongo, Transleqo, Hydroleap, OodlesAI, and Nokenchain. He has also demonstrated his capabilities in various other areas such as project management, requirement analysis, client communication, project execution, and team management. With his wide range of skills and experience, he can deliver exceptional results and add value to any organization he works with.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us