Register Custom Navigation Menus in WordPress

Navigation menus have been a theme feature since WordPress version 3.0. What it allows you to do is register custom navigation menus in a theme. This is useful if you want to add a menu to the sidebar, a sub-menu in the header, etc… To do this, we can use the registernavmenus function.

Creating the Function

First, you need to create a custom function. Place the function in the theme’s functions.php file:

// Register Custom Navigation Menus 
// Website: 
function register_my_menus() { register_nav_menus( array( 'top-menu' => __( 'Top Menu' ), 'menu-right' => __( 'Menu Pull Right' ), ) ); }
add_action( 'init', 'register_my_menus' );

In the example above, we are creating two (2) menus: Top Menu and Menu Pull Right. To add more menus or edit the ones I have, we have to look at the following lines:

 'top-menu' => __( 'Top Menu' ), 'menu-right' => __( 'Menu Pull Right' ),

This creates two (2) menus as previously stated, where ‘top-menu’ and ‘menu-right’ is the menu ID and ‘Top Menu’ and ‘Menu Pull Right’ is the name – it could also be a description. Make sure the comma is at the end of the line, including the last menu in the array.

Add to Your Theme

To add the navigation menu to your theme, we need to create a simple PHP statement that uses the wpnavmenu function. To add ‘top-menu’ to your website, the code would look like this:

 <?php wp_nav_menu( array( 'container' => false, 'theme_location' => 'top-menu', 'after' => '', 'items_wrap' => '%3$s' )); ?>

In the above code example, I’m stating that I don’t want a container (you can if you want), and to grap the menu I’m using the themelocation argument. Where the value of themelocation argument is the menu ID (not the name). You can read more about the function’s arguments here.