1. Register your menu
Register your menu with register_nav_menus()
after which you can choose that menu from admin area: Appearance -> Menus. Add one of these to functions.php
:
<?php
register_nav_menus( array( 'primary' => __( 'Main Menu', 'tt' ) ) );
//'primary' is basically like a slug that can be used to get that specific menu later on
//'Main menu' is the name that appears in admin area: Appearance -> Menus
//'tt' is your translation creds
//If you're not making your strings translatable:
register_nav_menus( array( 'primary' => 'Main Menu' ) );
?>
2. Echo your menu
Use wp_nav_menu()
to insert this menu anywhere you want, to header in your case. I’ve added references below, you can read about different parameters from there but I’ll add comments to important ones:
<?php
$defaults = array(
'theme_location' => 'primary', //Same slug as you used to register it
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0, //How many submenus: 0 if only main, -1 if all
'walker' => ''
);
wp_nav_menu( $defaults );
//If you want to keep everything default, you only need to add your menu slug:
wp_nav_menu( array( 'theme_location' => 'primary' ) );
//Just make sure to put wp_nav_menu() where you want your menu to appear
?>
3. Make your menu
Don’t forget to build/add your menu from admin menu: Appearance -> Menus
and make sure you’ll choose the one you registered in step one. You can customize, change, edit, add anything you need from there and it will update your menu automatically.
References:
register_nav_menus()
: https://codex.wordpress.org/Function_Reference/register_nav_menu
wp_nav_menu()
: https://codex.wordpress.org/Function_Reference/wp_nav_menu