Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #454119

    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

    #454652

    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

    • This reply was modified 9 years, 5 months ago by Elliott.
    #454689

    Thanks Elliott,

    I have tried that solution but now it is not changing the menu at all based on the cookie.

    #454929

    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

    #461460

    This worked! I set the cookie based on URL string.

    Thanks

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Different main menu based on cookie’ is closed to new replies.