Use WordPress shortcodes to embed a menu easily in just 5 minutes

WordPress Shortcodes for menus
WordPress Shortcodes make things easier

What are WordPress Shortcodes?

Shortcodes are simple lines of code that allow you to embed things into pages, posts or custom fields. The things can be plugins, other pages, images or anything.

But not everything can be embedded as a shortcode, you will need a custom bit of code to enable WordPress shortcodes.

In the following example, our Melbourn WordPress Developer has written a custom script that will allow you to use a menu shortcode anywhere on your WordPress website.

Add the following code to your functions.php file

function menu_shortcodes($atts, $content = null) {

extract(shortcode_atts(array( 'name' => null, 'class' => null ), $atts));
return wp_nav_menu( array( 'menu' => $name, 'menu_class' => $class, 'echo' => false ) );

}

add_shortcode('menu', 'menu_shortcodes');

Adding the WordPress Menu using the shortcode

Now you will be able to add a WordPress Menu write the following shortcodes anywhere on your WordPress website:

[menu name="my_menu" menu class="my_menu_class"]

This is quite an easy way to enable shortcodes without the need for embedding unnecessary plugins.