Navigation Menus in WordPress

WordPress Templates

A navigation menu is basically a region given a unique ID within the theme template which can have a menu assigned to it from the WP admin panel.

Register The Menu

  • All navigation menus must first be registered in the functions.php theme file.
  • Registering a menu is done with the register_nav_menu() function called in the after_setup_theme hook.
add_action( 'after_setup_theme', 'register_main_nav' );
function register_main_nav() {
    register_nav_menu(

        // The unique ID (slug) for the menu.
        'main_nav',

        // A short menu description and where it goes.
        // Wrapped with the text domain ID for the theme.
        __('Main Navigation', 'onetheme-text-domain')
    );
}

Once the menu is registered with the theme it will become available in the admin panel under Appearance > Menus > Manage Locations. An admin user can then assign an existing menu widget to the ID.

Display the menu

Define the section within a theme template file which will hold the menu.

<nav class="site-nav">
<?php wp_nav_menu(array(
    'theme_location' => 'main_nav' // The menu ID registered in functions.php.
)); ?>
</nav>