以编程方式设置 Wordpress 主菜单

Setting Wordpress Primary Menu Programmatically

我目前有两个通过后端创建的菜单。我需要根据地理位置激活菜单,我已将其设置为默认货币。后端的菜单名称是 Main 和 Main International。下面是代码:

function geo_client_currency($client_currency){
$userInfo = geoip_detect2_get_info_from_current_ip();

if ($userInfo->country->isoCode == 'US'){
    $client_currency = 'USD'; //currency code
    }
else {
    $client_currency = 'INR';
}

return $client_currency;
}

基本上我需要做的是为美国设置主菜单,为美国以外的任何地方设置主国际菜单。我已经查看了法典,但不太确定如何以最简单的方式实施它

在模板文件中可以像这样简单吗(如header.php)?

<?php
    $userInfo = geoip_detect2_get_info_from_current_ip();
    if ( $userInfo->country->isoCode == 'US' ){
        wp_nav_menu(array('menu' => 1));    //Change 1 to be the Main ID
    } else {
        wp_nav_menu(array('menu' => 2)); //Change 2 to be the Main International ID
    }
?>
<?php

$userInfo = geoip_detect2_get_info_from_current_ip();

if ( $userInfo->country->isoCode == 'US' ){
    wp_nav_menu( array( 'theme_location' => 'nav-menu', 'menu'=> 'Main' , 'depth' => 3, 'container' => false, 'menu_class' => 'sf-menu', 'walker' => new thb_MegaMenu  ) );
} else {
    wp_nav_menu( array( 'theme_location' => 'nav-menu', 'menu'=> 'Main International' , 'depth' => 3, 'container' => false, 'menu_class' => 'sf-menu', 'walker' => new thb_MegaMenu  ) ); //Change 2 to be the Main International ID
}

?>