Hello,
I have been able to create a different menu based on a specific cookie for our site. However, I am running into the problem where it does not replace or append a menu to only a specific location (the main menu) but will propagate the menu to ALL available menu locations on a given page. Please review my code below and suggest how I can replace the menu based on the cookie for ONLY the main header menu.
add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args_filter');
function my_wp_nav_menu_args_filter($args = array())
{
if ($_COOKIE['division'] == 'west' )
{
$args['menu'] = 'West Main Menu';
}
return $args;
}
Thanks,
Chad
Hi sooter8!
I didn’t test this out but try it instead.
add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args_filter');
function my_wp_nav_menu_args_filter($args)
{
if ($_COOKIE['division'] == 'west' && $args['theme_location'] == 'avia' )
{
$args['menu'] = 'West Main Menu';
}
return $args;
}
“avia2” and “avia3” will target the secondary and footer menus.
Regards,
Elliott
Thanks Elliott,
I have tried that solution but now it is not changing the menu at all based on the cookie.
Hi!
Use the menu id instead of the menu name.
add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args_filter');
function my_wp_nav_menu_args_filter($args)
{
if ($_COOKIE['division'] == 'west' && $args['menu_id'] == 'avia-menu' ) {
$args['menu'] = '7';
}
return $args;
}
Change 7 to the menu id of the “West Main Menu”. How did you set the cookie?
Cheers!
Ismael
This worked! I set the cookie based on URL string.
Thanks