I created following shortcode function to call a menu on a specific location on page
function servicesmenu() {
return wp_nav_menu( array( 'theme_location' => 'services_nav', 'menu_id' => 'servicesmenu') );
}
add_shortcode('servicesMenu', 'servicesmenu');
Above code is working fine. But when i tried to wrap the shortcode in a DIV it’s not wrapping, the menu generated by shortcode is coming before div id=”services_rightarea”
<div id="services_rightarea">
<!-- other content here -->
[servicesMenu]
</div>
Any solution please
The problem is that the
wp_nav_menuechos by default. You need to capture this echo in a variable and return it. This can be done with output buffering:Edit: I just noticed that you’re also using the
theme_locationproperty. This will try to insert the result of thewp_nav_menucall intoservicesmenumenu, which was registered with register_nav_menu().Take the
theme_locationparameter out and the output buffering will work as expected.