How To Create Navigation menu In WordPress Theme Within A Minute?
September 14th, 2016 | by Ravi Chahar || 6 Comments |
When you develop a child theme or any WordPress theme then you may in need to create navigation menu in WordPress theme. It’s one of the important parts of the website.
Are you thinking about adding the theme support option to show the navigation menu to your website? Well, for the navigation menu, you have to register it using a function. If you are a blogger then you just have to copy and paste the code.
Though most of the web developers try their best to create navigation menu in WordPress theme so that the users won’t face any problem.
But in case, if you want to add it yourself then you have to create a function to register the menu to WordPress.
What To Do To Create Navigation Menu In WordPress Theme?
If you know the WordPress theme file structure then you would know about the functions.php file. In this process, you have to use this file.
There are some steps to take.
Step 1:- First of all, you have to create a function to register the navigation menu.
register_nav_menu(‘new menu’, __( ‘New Menu’ ));
add_action( ‘init’, ‘your_nav_menu’ );
In th above code “your_nav_menu” is the name of the function. You can replace the name of the function according to your theme. “New Menu” is the name appear in your WordPress “Menus” section for this navigation menu.
Step 2:- Now you have to place the navigation menu where you want to show it to your WordPress theme. If you are creating the header navigation menu then put it in the header.
If you want to add it in the footer then this can also be done.
<?php wp_nav_menu( array ( ‘theme_location’ => ‘new menu’)); ?>
You can place this code in any section of your WordPress theme. Just make sure that you use the same name which is registered. Only the PHP code would work for showing the menu. But you know that HTML is necessary to embed the PHP codes.
As you can see in the above function, you have registered the “new menu“. So you have to call the same name wherever you want to show the navigation menu.
If you change the name then nothing will happen. It is case sensitive, so make sure that you don’t use upper words instead of lower.
You Can Add More Than One Navigation Menu In WordPress.
You may have read that people used to ask if they can add more than one navigation menu in their theme. Well, the solution is here.
You just have to do the small change in the above-mentioned codes. While registering, you have to register all the navigation menus. The same while calling them.
Let me show you how.
Step 1:- Register all the navigation menus together in the same function.
‘primary’ => __(‘Header Menu’),
‘footer’ => __(‘Footer Menu’),
Here, two navigation menus are registered. you can add as many navigation menus you want. Just add an additonal line of the code for each menu.
Step 2:- Now you have to place the code to show these navigation menus.
$args = array( ‘theme_location’ => ‘primary’);
<?php wp_nav_menu( $args); ?>
Using this code, you can call the first menu. The same code can be used for the second and all the menus you would create. Just replace the name of the menu with the name you registered.
As I have described in the single menu, you can also use that code. In this code, I have passed an argument for the better coding.
Are You Ready To Create Navigation Menu In WordPress Theme?
The above codes would do the work for you. It depends on the way a developer provides the code. I have mentioned the arguments. But there are many developers who prefer some other way.
The main point is to create navigation menu in WordPress theme. It can be fulfilled using these codes. If you want to show two menus together then just call the registered navigation menus.
In a WordPress theme, it’s very important to guide the readers about the topics to be covered. You can show many menus to your website. If you still face any problem then feel free to ask.