Super stuff. I asked a dev friend of mine and he wrote something.
All you have to do is change “menu_in_post_menu” TO “menu_in_post_dropdown” in the shortcode
// ================================ START ================================
add_shortcode('menu_in_post_dropdown', 'menu_in_post_output_dropdown');
function menu_in_post_output_dropdown($atts = array(), $content, $shortcode_tag) {
if (isset($atts['menu'])) {
$menu = absint($atts['menu']);
} else {
$menu = 0;
}
if ($menu == 0) {
return menu_in_post_fallback_fxn();
} else {
$args = array('menu'=>$menu, 'fallback_cb'=>'menu_in_post_fallback_fxn', 'echo'=>false);
}
// If menu_id is empty, don't pass a value, and the menu slug with an incremented value added will be used.
// If container_class is empty, don't pass a value and 'menu-{menu slug}-container' will be used.
$defaults = array(
'menu_class'=>'menu',
'menu_id'=>'',
'container'=>'div',
'container_class'=>'',
'container_id'=>'',
'depth'=>0
);
$search = array('<script>', '</script>', '<?php', '?>');
foreach ($defaults as $att=>$default) {
switch($att) {
case 'depth':
if (isset($atts[$att])) {
$passed_depth = absint($atts[$att]);
if ($passed_depth > 0) {
$args['depth'] = $passed_depth;
}
}
break;
// These should be only strings.
default:
if (isset($atts[$att])) {
$passed_att = filter_var($atts[$att], FILTER_SANITIZE_STRING);
if ($passed_att != '') {
$args[$att] = $passed_att;
}
}
}
}
return print_menu_shortcode($args, $atts['menu']);
}
function print_menu_shortcode($atts, $menuId) {
extract( shortcode_atts(
array(
'name' => null,
'class' => null
),
$atts
));
// Assuming $name contains slug or name of menue
$menu_items = wp_get_nav_menu_items($menuId);
// Sample Output. Adjsut as per your exact requirements.
// Sample Output variable.
$menu_dropdown = '';
if ($menu_items){
$menu_dropdown .= '<select onChange="document.location.href=this.options[this.selectedIndex].value;">';
foreach( $menu_items as $menu_item ) {
$link = $menu_item->url;
$title = $menu_item->title;
$menu_dropdown .= '<option value="' . $link .'">'. $title . '</option>' ;
}
$menu_dropdown .= '</select>';
} else {
$menu_dropdown = '<!-- no menu defined in location "'.$theme_location.'" -->';
}
return $menu_dropdown ;
}
// ================================ END ================================
https://prnt.sc/qo54y7 – Shows it in action.
I just put # for container and stacked in wordpress menus as you normally do.
Maybe this is helpful?
-
This reply was modified 4 years, 10 months ago by kingfisher64. Reason: code highighting
-
This reply was modified 4 years, 10 months ago by kingfisher64.