I'm using the following function that I found online to render each page's individual nav items to the page:
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
)
);
}
I would like to display these nav links in a dropdown menu with a button labeled Menu instead of just having the links render as an ul
, but I don't know how to access the individual items within wp_nav_menu
.
Any help would be the best, thanks!
I'm using the following function that I found online to render each page's individual nav items to the page:
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
)
);
}
I would like to display these nav links in a dropdown menu with a button labeled Menu instead of just having the links render as an ul
, but I don't know how to access the individual items within wp_nav_menu
.
Any help would be the best, thanks!
Share Improve this question edited Mar 29, 2019 at 4:33 Sven 3,6841 gold badge35 silver badges48 bronze badges asked Mar 28, 2019 at 23:29 Brianne DuffyBrianne Duffy 131 silver badge3 bronze badges1 Answer
Reset to default 1You can use wp_get_nav_menu_items() which retrieves all menu items of a navigation menu. Your shortcode callback should be something like this .
function print_menu_shortcode($atts, $content = null) {
extract( shortcode_atts(
array(
'name' => null,
'class' => null
),
$atts
));
// Assuming $name contains slug or name of menue
$menu_items = wp_get_nav_menu_items($name);
// 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 ;
}
More Examples on use of this function.