WP Menu Register
How to register menu in wordpress
Welcome to everybody. Today I am going to describe how register menu in wordpress to use dynamically. From this tune everyone can dynamic their HTML menu in wp easily.
Step one: In this step you have need register menu in theme function.php file. To register menu take a function here name “various_nav_menus” this all need to add_action as like other function. Here register menu id is “main-menu”
add_action('init', 'various_nav_menus'); function various_nav_menus() { if (function_exists('register_nav_menu')) { register_nav_menu( 'main-menu', __( 'Main Menu', 'varioustheme' ) ); } } function various_default_nav() { echo '<ul class="nav navbar-nav">'; if ('page' != get_option('show_on_front')) { echo '<li><a href="'. home_url() . '/">Home</a></li>'; } wp_list_pages('title_li='); echo '</ul>'; }
You may saw two function in above code. The function taking for default menu if use not set first register menu than will be show as menu. If you want to get same style in default menu also you have must to use you “ul” css class. And next step you need to use the register menu in your menu position.
Step two: In this step you need to call register menu by using register menu id “mail-menu0” and here use ‘menu_class’ => ‘nav navbar-nav’ is your html menus ul class used.
<?php if (function_exists('wp_nav_menu')) { wp_nav_menu(array('theme_location' => 'main-menu', 'menu_class' => 'nav navbar-nav', 'fallback_cb' => 'various_default_nav')); } else { various_default_nav(); } ?>
Using the code instead of your all ul element or cut all <ul> to </ul> use above code in this position. Now your wp menu working dynamically use the menu from appearance to menu and enjoy.
If you do not want to used default menu you can use only first function in function.php like below:
add_action('init', 'various_nav_menus'); function various_nav_menus() { if (function_exists('register_nav_menu')) { register_nav_menu( 'main-menu', __( 'Main Menu', 'varioustheme' ) ); }
And use below code instead of ul or ul position:
<?php wp_nav_menu( array( 'theme_location' => 'main-menu', 'menu_class' => 'nav navbar-nav') ); ?>
Comments are closed