0
votes

I want to assign different menus based on a user's location. I've installed the GeoIP Detection plugin that detects a user's country code. I assigned the variable $userInfo = geoip_detect2_get_info_from_current_ip() in functions.php and verified it works. I also created two different menus called 'Main Menu' and 'Main Menu International'.

By default, the menu "Main Menu International" (ID 303) is assigned to theme locations "Main Menu" and "Main Menu - Mobile".

But how do I assign the menu "Main Menu" (ID 51) to these two theme locations when isoCode = "TH"?

Here's what I have so far. I need help with figuring out where to put this code, and how edit it to make it assign the menu to both "main" and "mobile" locations.

<?php
    if ( $userInfo->country->isoCode == 'TH' ){
        wp_nav_menu(array('menu_id' => 51)); //Main Menu ID
    } else {
        wp_nav_menu(array('menu_id' => 303)); //Main Menu International ID
    }
?>

Edit: Getting closer (I think). I replaced the original code with the code below but now the menu with the lower id shows regardless of country or what menu is enabled in the WP admin panel. No matter what parameters in the code I change it still shows the menu with ID 51.

<?php
    if ( $userInfo->country->isoCode == 'TH' ){
        wp_nav_menu(array(
        'menu_id'       => 51,
        'container'     => false,
        'items_wrap'    => '%3$s',
        'depth'         => 0,
        'walker'        => new FlatsomeNavDropdown)); //Main Menu
    } else {
        wp_nav_menu(array(
        'menu_id'       => 303,
        'container'     => false,
        'items_wrap'    => '%3$s',
        'depth'         => 0,
        'walker'        => new FlatsomeNavDropdown)); //Main Menu International
}
?>

I'm wondering if I need to replace the number I'm using in menu_id with the text slug of the menu id. But I can't seem to find this.

2
You need to wrap the IF statement around the current menu code and then put the new menu code in the ELSE statement.Austin Collins

2 Answers

1
votes

It depends on where you want to place these menus at. If you want to put them in your header, then your theme file 'header.php' is the place to go. If you want it in sidebar, then go to sidebar.php (or what's equivalent) and the test will work well

0
votes

Menu code looks fine , it could be the geolocation plugin what it's failing. You could try with Geotargeting WordPress which I used many times to achieve what you trying to do.

And a plus is that supports cache plugin by using javascript + ajax