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' );

Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.

Recent Posts

  1. Disabling Payment Methods in WooCommerce Based on Conditions
  2. How to Update Product Quantity in WooCommerce Using Custom Code
  3. Dynamically Generating a Table of Contents in WordPress
  4. Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
  5. Understanding the Impact of git reset --hard Command

Your Questions / Comments

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