$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>query - How to pass menu slug as a variable to wp_nav_menu?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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' );
Post a comment

comment list (0)

  1. No comments so far