Making a wordpress dynamic nav menu :

Making a wordpress dynamic nav menu is very easy. You can easily make a dynamic nav menu. Dynamic nav menu is very useful and effective for a both wordpress theme and sites. If you add dynamic nav menu in your wordpress sites. Then user can easily maintain his nav menu. Without dynamic nav menu you will not able to submit your wordpress theme in the themeforest. So it is very important for a wordpress theme. There is many example in the codex.

So for this first you have to add following code in the functions.php

function flexit_theme_menus(){
 register_nav_menus(array(
 'main_menu'=> 'Main Menu'
 ));
 }
 add_action('init','flexit_theme_menus');

Here main_menu is the class of the menu that you have made. Will help to add class in the element of the menu.

For a fallback menu add the following code also. It means when your doesn’t set a menu then the default menu will be shown

function flexit_fallback_menu(){
 echo'<ul id="menu">';
 if('page' != get_option('show_on_front')) {
 echo '<li><a href="'.site_url().'/">Home</a></li>';
 }
 wp_list_pages('title_li=');
 echo'</ul>';

Here menu is also the class of the menu. Will help to get the style of the menu.

Then add the following code in the required place of your menu.

<?php wp_nav_menu(array(
 'theme_location' => 'main_menu',
 'menu_id' => 'menu',
 'fallback_cb' => 'flexit_fallback_menu'
 ))?>

Here theme_location means where will be the code will shown. And menu id is the menu id and fallback_cb if you want to set default nav menu. If you don’t want default nav you need not to add that.

Then set the menu in the dashboard like the below picture.

56

If you face any problem to adding this please add a comment I must give a reply. I will be grateful if I help you. So feel free to ask a question.

You may also like

Adobe Photoshop tutorial

 

Useful Tags :