What is WordPress Menu
WordPress is having Navigation Menu often known as WordPress Menu, which is a basic theme feature introduced in erlier WordPress version 3.0. WordPress this new feature provide us, an easy to use mechanism for implementing customised navigation menus into our created themes. In order to incorporate menu in your theme, We need to add a few code segments to your theme files. Which help us to create a menu. register_nav_menu() will register single menu area this is useful when you are just having single menu in your website.
function register_new_menu() { register_nav_menu('Main-menu',__( 'Main Menu' )); } add_action( 'init', 'register_new_menu' );
But if you having multiple menu in website than you need to create more than one menu area that can be possible using register_nav_menus().
function register_my_menus() { register_nav_menus( array( 'header-menu' => __( 'Header Menu' ), 'extra-menu' => __( 'Extra Menu' ) ) ); } add_action( 'init', 'register_my_menus' );
Once you are register with menu next part is to show the menu lets see how to show the menu?
How to show the Menu using the Register Area in WordPress?
TO show the Menu in your theme you need to create a menu
- Create a menu, Just go to Appearance » Menus
- if This is first menu than click on the
- Else the will be the next to the menu drop-down selection.
- Enter the Menu name this should be unique and different from the previous.
- Once Menu is created add the Menu item by selecting the check box and adding to the Menu, don’t worry about the order of the menu that can be changed later on.
- Once you Menu item are done, just check the Menu area or Theme locations, from the bottom checkbox where you want to show.
- All set now save the menu.
- Now you can see your Selected item are in the Menu area of your selected theme.
How to display Menu in WordPress Themes?
To show the Menu in your theme you need to create a menu area which has been explained before, Once you have created the menu are and added the menu item you need to show it into the WordPress theme.
wp_nav_menu( array( 'theme_location' => 'header-menu', 'container_class' => 'my_extra_menu_class' ) );
This code will show menu item of the Register Header Menu on the Selected location
How to get The WordPress Menu from Menu ID
Many time you need to create dynamic secondary menu in WordPress, you already save the menu id or say term_id (as Menu is stored under the terms table in WordPress) in your post_meta table and now you want to change menu on each page than what to do? confusing?? Don’t bother just chill, we are having a simple code which will show the WordPress Menu from the use of the term_id.
First you need to get the menu term_id using the get_post_meta($postid,'post-meta-name',true); or get_fields('post-meta-name',$postid) and than you need to get_nav_menu_object()
<?php $mymenu = get_field('menu_to_be_show'); $my_menu = wp_get_nav_menu_object($mymenu); // This will get our menu data. $nav_menu_args = array( 'fallback_cb' => '', 'menu' => $my_menu ); wp_nav_menu($nav_menu_args); // This will display and render our menu.