shortcode – wpv-menu

Sometimes it is useful to include a WordPress Menu in a Content Template or a View. It is possible to build a shortcode to display it:

// Display WP Menu shortcode 

add_shortcode('wpv-menu', 'print_menu_shortcode');
function print_menu_shortcode($atts, $content = null) {
  extract(shortcode_atts(array( 'name' => null, 'class' => null ), $atts));
  return wp_nav_menu( array( 'menu' => $name, 'menu_class' => $class, 'echo' => false ) );
}


wp_nav_menu() reference: https://developer.wordpress.org/reference/functions/wp_nav_menu/

Then I can use this shortcode anywhere:

[wpv-menu name="main-menu"]


It is also possible to replace “main-menu” with any menu’s slug.

You can find this solution on this Toolset forum thread:

http://stephanieleary.com/2010/07/call-a-navigation-menu-using-a-shortcode/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>