Register WordPress navigation menu and insert in theme

Navigation menu is an important part of any website. Without navigation menus a website can not be looks good and it looks like the website is not properly arranged.

WordPress has a nice navigation system that you can find in Appearance>>Menus. From here you can add menu to your navigation. Almost all the WordPress theme has already registered navigation menus.

WordPress allows developers to register navigation menus with a nice way. WP always stand as a developers friendly CMS application and in the case of navigation menus also there are no exceptional. You just need to put few lines of code and your menu location will registered successfully. So how you can register navigation menus in your own WP theme? In this tutorial I am going to show you the code which will register navigation menu and you will be able to show it in your theme.

Register Navigation Menus

register_nav_menus is the WordPress function which will let you register menus in your theme. Registers multiple custom navigation menus in the new custom menu editor introduced in WordPress 3.0. This allows WordPress developers for the creation of custom navigation menus in the dashboard for use inside theme. Below is the usage of this function:



<?php register_nav_menus( $locations ); ?> 

The code for creating single navigation menu is:

register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );

But you can also put a array of menus for multiple menu location. Here is the code below which will register new navigation menu in your theme:

register_nav_menus( array(
    'primary' => __( 'Primary Menu', 'THEMENAME' ),
) );

Insert the above code in your theme’s functions.php file and you will able to see the menu in display location under Appearance>>Menus like the picture below:

Was that not rally so easy? Adding just a little bit of PHP code inside functions.php display a theme location.

You can add more menu location as an array. Here is the modified code to show you how to add more than one menu location:

register_nav_menus( array(
    'primary' => __( 'Primary Menu', 'THEMENAME' ),
    'footer' => __( 'Fix footer', 'THEMENAME' ),
) );

now you will see two menu location like the picture given below:

Add Registered Navigation Menu In Theme

Now how to display the navigation menu on your site through theme? You need to provide a code into your theme’s template file which will display the navigation menus. In most of the theme the code for displaying navigation menus inserted inside header.php file of the theme. Here is the PHP code below that you need to put in your theme’s template:

<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

It will add the menus on your WordPress site through theme. Now you will be able to see it.

 

WordPress is really so nice. Isn’t it? WordPress has nice environment for developers and it allows to do many other things that is really so interesting to know.

Leave a Reply

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