Add custom menu to WordPress theme

Lets say you have a web site with many pages. By default those pages appear in chronological¬†order, which might not be what you want. In that case you need to go to Appearance – > Menus. If the theme you are using hasn’t defined navigation menus, you would have to change the date when your pages are published in order to place them in the right order. Much easier would be to register navigation menus in your theme. Here is how.

In your theme functions.php file add the following lines:

if ( function_exists( 'register_nav_menus' ) ) {
	register_nav_menus(
		array(
		  'custom_menu' => 'Main navigation area',
		)
	);
}

What’s important to know from this code is the name you define to custom_menu (Main navigation area). That name can be used let’s say in your header.php or any other file of your theme in the following way.

<nav class="menu">
	<?php wp_nav_menu(array('menu' => 'custom_menu')); ?>
</nav>

After you add that code, you will be able to define custom menus through Appearance in the admin area.

Function Reference/register nav menus – WordPress Codex

Leave a Reply

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