How to Create Menu Shortcode Without Plugin in WordPress

Bhuvi: Hey… I am looking for something to display menus that I choose on the WordPress page or post on the fly…is it possible?.

DIWP: Yup Man…of Course. You can do that by just creating a simple shortcode for the menu and then just add that shortcode in the page or post where you want to display the menu.

Bhuvi: Sounds Interesting but how to do that?

DIWP: Don’t worry, Here is the code snippets that will create a shortcode for the menu in WordPress. Just add this code in your theme’s function.php file.

//Shortcode to Display Menus

function diwp_menu_shortcode($attr){

	$args = shortcode_atts(array(

				'name'  => '',
				'class' => ''

				), $attr);

	return wp_nav_menu( array(
				'menu' 	        => $args['name'],
				'menu_class'    => $args['class']
add_shortcode('addmenu', 'diwp_menu_shortcode');

Bhuvi: Ok, I have added the code in my theme’s functions.php file. Now what next?

DIWP: Now add the shortcode [addmenu name=”Your Menu Name”] in your page or post or wherever you want to display menu in your WordPress website.

Bhuvi: Okay…but where can I find my menu name?

DIWP: Don’t worry…Man. I am here to help you. Just go to Appearance > Menu and find menu name as shown in the image below. Just copy that menu name from there and add it to your shortcode.

So in this case, it will be something like [addmenu name=”Primary Menu”]


Bhuvi: Ok, I got it. Can I add class using this shortcode?

DIWP: Yes, you can add a class name for the menu by using attribute class=”your-class-name” in the shortcode.

Bhuvi: I think I got what I was looking for.

DIWP: Thanks, Keep Visiting Me DIVE IN WP. You will find really helpful information about WordPress.

Bhuvi: Yes.. Of Course I will.

DIWP: Thanks,

Share it to the Community!

You May Also Like These shortcode Posts:

4 Replies to “How to Create Menu Shortcode Without Plugin in WordPress”

  1. Thank you, Thank you, Thank you, a million times Thank you. This works and is exactly what I needed. You explained it very well so that it is easy to understand.

  2. Hi,
    It works great but displays menu as a list with all the sub menus.
    How can I make the sub menu item to appear after hover or clicking the main item.

    1. Hi Swapnil,

      To create submenu items to be appear on hover or clicking the main item, you can use CSS or JavaScript to show and hide them on hover of the main item.

Comments are closed.