Skip to content

Instantly share code, notes, and snippets.

@agrogeek
Created January 23, 2020 08:31
Show Gist options
  • Save agrogeek/d3c2d455ef4c34e506503f6b4e44e142 to your computer and use it in GitHub Desktop.
Save agrogeek/d3c2d455ef4c34e506503f6b4e44e142 to your computer and use it in GitHub Desktop.
Add a menu using shortcode. Usage: [print-menu name = "menu-name" class = "class1 class2"]. Defaults: name='primary', class=''.
<?php
function syltec_print_custom_menu_shortcode($atts)
{
// Normalize
$atts = array_change_key_case((array)$atts, CASE_LOWER);
$atts = array_map('sanitize_text_field', $atts);
// Attributes
$menu_name = isset($atts['name'])?$atts['name']:'primary';
$menu_class = isset($atts['class'])?$atts['class']:'';
return wp_nav_menu(array(
'menu' => esc_attr($menu_name),
'menu_class' => 'menu ' . esc_attr($menu_class),
'echo' => false));
}
add_shortcode('print-menu', 'syltec_print_custom_menu_shortcode');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment