以编程方式设置 WordPress 主菜单


Setting Wordpress Primary Menu Programmatically

我目前通过后端创建了两个菜单。我需要根据地理位置激活菜单,我已经进行了设置以设置默认货币。后端的菜单名称是"主"和"国际主"。下面是代码:

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;
}

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

<?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
}

?>

在模板文件中可以这么简单吗(如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
    }
?>