数组变量$selected_locations
包含值TH
,字符串变量$my_location
设置为'TH'
。
我使用以下方式验证了这一点:
<?php
$selected_locations = get_post_meta( $item_id, 'locations', true );
print_r($selected_locations);
$user_location = geoip_detect2_get_info_from_current_ip();
$my_location = $user_location->country->isoCode;
echo $my_location;
?>
然而,行
if( in_array($user_country_code, $selected_locations, true ) )
$visible = false;
在下面的函数中不起作用。
function visibility_check( $items, $menu, $args ) {
$user_location = geoip_detect2_get_info_from_current_ip();
$user_country_code = $user_location->country->isoCode;
$selected_locations = get_post_meta( $item_id, 'locations', true );
$hidden_items = array();
foreach( $items as $key => $item ) {
$item_parent = get_post_meta( $item->ID, '_menu_item_menu_item_parent', true );
if( in_array($user_country_code, $selected_locations, true ) )
$visible = false;
else
$visible = true;
if( ! $visible || isset( $hidden_items[$item_parent] ) ) { // also hide the children of unvisible items
unset( $items[$key] );
$hidden_items[$item->ID] = '1';
}
}
return $items;
}
如果我手动将TH
分配给数组变量$selected_locations
,它就可以正常工作。
有什么建议吗?
编辑:
我刚刚意识到数组正在Array ( [0] => 'TH' )
下面的函数中返回数据,但在上面的函数中返回空。 Array ( [0] => )
function option( $fields, $item_id ) {
ob_start(); ?>
<p class="field-visibility description description-wide">
<label for="edit-menu-item-visibility-<?php echo $item_id; ?>">
<?php _e('Enter country code(s) separated by commas') ?>:
<input
type="text"
class="widefat code"
id="edit-menu-item-visibility-<?php echo $item_id; ?>"
name="menu-item-visibility[<?php echo $item_id; ?>]"
value="<?php echo esc_html( get_post_meta( $item_id, 'locations', true ) ); ?>" /></br>
<input
type="radio"
id="edit-menu-item-visibility-<?php echo $item_id;?>"
name="menu-item-show-hide[<?php echo $item_id; ?>]"
value="hide" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'hide', true ); ?>
/>Hide from these locations.</br>
<input
type="radio"
id="edit-menu-item-visibility-<?php echo $item_id; ?>"
name="menu-item-show-hide[<?php echo $item_id; ?>]"
value="show" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'show', true ); ?>
/>Only show to these locations.</br>
<?php
$locations_string = esc_html( get_post_meta( $item_id, 'locations', true ) );
$locations_array = explode(',', $locations_string);
$locations_array_trimmed = array_map('trim', $locations_array);
print_r($locations_array_trimmed);
$user_location = geoip_detect2_get_info_from_current_ip();
$user_country_code = $user_location->country->isoCode;
echo $user_country_code;
?>
</label>
</p>
<?php
$fields[] = ob_get_clean();
return $fields;
}
答案 0 :(得分:0)
我正在宣布我的变量超出范围。重新排列东西有点固定它。
function visibility_check( $items, $menu, $args ) {
$hidden_items = array();
foreach( $items as $key => $item ) {
$user_location = geoip_detect2_get_info_from_current_ip();
$user_country_code = $user_location->country->isoCode;
$selected_locations = get_post_meta( $item_id, 'locations', true );
$item_parent = get_post_meta( $item->ID, '_menu_item_menu_item_parent', true );
if( in_array($user_country_code, $selected_locations, true ) )
$visible = false;
else
$visible = true;
if( ! $visible || isset( $hidden_items[$item_parent] ) ) { // also hide the children of invisible items
unset( $items[$key] );
$hidden_items[$item->ID] = '1';
}
}
return $items;
}