How to make WordPress dynamic menu or nav bar

Hi,in this post How to make a WordPress dynamic menu . I am going to show you how to declare menus in functions.php file and fetch the menu in frontend.

Now start to make a dynamic menu step by step

Step 1. Declare menu in functions.php file

Sample code:

//To register nav menu
function register_my_menus() {
$menu_args = array(
'main_menu' => __( 'Main Menu' ),
'footer_menu' => __( 'Footer Menu' )
);
register_nav_menus($menu_args);
}

add_action( 'init', 'register_my_menus' );

we have declare 2 menus here one for header or main menu and another is for footer menu

here the “’main_menu” and  “footer_menu” will be used as id to get the menu in frontend , and the “Main Menu” and “Footer Menu” is the name of the menu van be seen from the admin panel of wordpress.

Step 2. Create 2 menu from admin panel and check the boxes for the regarding menu

Menu

Menu

Step 3. Now get the menu from frontend, and paste the code to header.php or some other place from where you want to fetch the main menu,

Code for getting the menu from the frontend

<?php
//The Arguments of the menu
$args = array(
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'item_spacing' => 'preserve',
'depth' => 0,
'walker' => '',
'theme_location' => 'main_menu'                           
);

//to display the menu
wp_nav_menu( $args );
?>

Here to get the menu the argument ‘theme_location’ => ‘main_menu’ is mandatory to get the regarding menu. And the “’main_menu” is the id of the menu you have declared in functions.php files , similarly change paste the code to footer.php of some other place you want to fetch the menu and change the “’main_menuc” to “footer_menu”

 

In this post we have learned to develop the dynamic menu for wordpress, In our next post we will learn about creation of bootstrap menu of wordpress.

Thanks for reading

Share The Post On -

  1. Badal says:

    Its a useful content to make a wordpress menu

  2. pratik says:

    this dynamic code is not working after adding code into a function.php file. The menu is not displayed in appearances section. Can you tell me the solution?

    • Code Mystery says:

      to show in appearance -> menu
      write the code:

      //To register nav menu
      function register_my_menus() {
      $menu_args = array(
      'main_menu' => __( 'Main Menu' ),
      'footer_menu' => __( 'Footer Menu' )
      );
      register_nav_menus($menu_args);
      }
      
      add_action( 'init', 'register_my_menus' );
      

      in functions.php and then follow the code to fetch in front end

    • Code Mystery says:

      the filename must be functions.php inside your theme folder not in root folder to write the code

Add a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.