WordPress shortcode to output wp_nav_menu by menu name/slug or id
A simple and clean way to create a usable shortcode to output menu created in WordPress in content
This is intended to be a plugin. Do not use functions.php for this
if ( ! class_exists( 'Maksimer_Shortcode_WP_Nav' ) ) :
class Maksimer_Shortcode_WP_Nav {
public function __construct() {
add_shortcode( 'navigation', array( &$this, 'maksimer_shortcode' ) );
}
public function maksimer_shortcode( $atts ) {
extract( shortcode_atts(
array(
'menu' => '',
), $atts )
);
if ( isset( $menu ) ) {
$args = array(
'menu_class' => 'content-menu',
'menu' => $menu,
'container' => 'nav',
'container_class' => 'content-menu-container',
'echo' => false,
);
return wp_nav_menu ( $args );
}
}
}
$maksimer_shortcode_wp_nav = new Maksimer_Shortcode_WP_Nav();
endif;
Add the following shortcode to the wysiwyg editor: [navigation menu=”$menu”] where $menu can be the menu name, slug or id set in WordPress admin.
This outputs a regular WordPress menu, wrapped inside <nav></nav>
Classes included for styling with css:
- nav element: content-menu-container
- ul element: content-menu