query - How to pass menu slug as a variable to wp_nav_menu?

admin2025-06-04  1

I need to show a menu depending on what user selects on Page Options.

I take menu name from a user to a variable and now i need it to parse through wp_nav_menu.

Im trying it like this but it doesn`t work:

$var = the_field('menu2');

$params = array(
    'theme_location'  => '',
    'menu'            => $var, // <== This is what i want to do
    'container'       => 'div',
    'container_class' => '',
    'container_id'    => '',
    'menu_class'      => 'menu',
    'menu_id'         => '',
    'echo'            => true,
    'fallback_cb'     => 'wp_page_menu',
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'depth'           => 0,
    'walker'          => ''
);

wp_nav_menu( $params );

I have also tried

'menu'   =>  '$var'

If someone could help me i would appreciate

Thanks

I need to show a menu depending on what user selects on Page Options.

I take menu name from a user to a variable and now i need it to parse through wp_nav_menu.

Im trying it like this but it doesn`t work:

$var = the_field('menu2');

$params = array(
    'theme_location'  => '',
    'menu'            => $var, // <== This is what i want to do
    'container'       => 'div',
    'container_class' => '',
    'container_id'    => '',
    'menu_class'      => 'menu',
    'menu_id'         => '',
    'echo'            => true,
    'fallback_cb'     => 'wp_page_menu',
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'depth'           => 0,
    'walker'          => ''
);

wp_nav_menu( $params );

I have also tried

'menu'   =>  '$var'

If someone could help me i would appreciate

Thanks

Share Improve this question edited May 14, 2015 at 18:29 TheDeadMedic 36.7k9 gold badges68 silver badges102 bronze badges asked May 14, 2015 at 17:12 Pito AxiuPito Axiu 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You want get_field() (which returns the value), not the_field() (which echo's the value):

$var = get_field( 'menu2' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748990165a315432.html

最新回复(0)