Sarathlal N

Create custom menus for WordPress themes

We can easily make & use new custom menus in our WordPress themes. Now I just walk through on different stages in this process.

First we want to register our new menu in our theme’s functions.php file.

function register_my_menu() {
 register_nav_menu('my-menu',__( 'My Menu' ));
}
add_action( 'init', 'register_my_menu' );

Then to display this menu, we want to tell the theme where we like the menus to show up. It may be any template file like header.php, footer.php etc according to our design.

So just add a line of code in appropriate file to show this new menu in our pages.

<?php wp_nav_menu( array( 'theme_location' => 'my-menu' ) ); ?>

If you like some wrapper for our new menu, just add an additional array element with this WordPress function.

<?php wp_nav_menu( array( ‘theme_location’ => ‘my-menu’, ‘container_class’ => ‘my_extra_menu_class’ ) ); ?>

Now our new menu is wrapped within a container with a class named as my_extra_menu_class.

Same way, making multiple custom menus is also too simple.

function register_my_menus() {
 register_nav_menus(
 array(
 'header-menu' => __( 'Header Menu' ),
 'extra-menu' => __( 'Extra Menu' )
 )
 );
}
add_action( 'init', 'register_my_menus' );

Recent Posts

  1. Automating Release Generation with GitHub Actions
  2. WP CLI Commands to Bulk Delete Entries in WordPress Database
  3. Split a Single CSV File into Multiple Files Using the Split Command - Bash
  4. Migrating code repo from BitBucket to GitHub
  5. Streamlining Development - Our Journey with Git, Bitbucket, and Jira

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.