我有一个名为" event"的自定义帖子类型有纬度和经度的acf。我想要争取所有"事件的距离"与帖子有关 我使用geoplugin.com来吸引用户Lat&长。
$geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
$geoiix = $geoplugin['geoplugin_regionName'] ;
$user_lat = $geoplugin['geoplugin_latitude'];
$user_long = $geoplugin['geoplugin_longitude'];
$artist_id = $tags[0]->term_id;
$args = array(
'post_type' => 'events',
'orderby' => 'meta_value',
'meta_key' => 'event_date',
'tag__in' => array( $artist_id ),
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$event_lat = get_field( 'gp_latitude' );
$event_long = get_field( 'gp_longitude' );
$earth_radius = 3960.00; # in miles
$lat_1 = $event_lat;
$lon_1 = $event_long;
$lat_2 = $user_lat;
$lon_2 = $user_long;
$delta_lat = $lat_2 - $lat_1 ;
$delta_lon = $lon_2 - $lon_1 ;
function distance_haversine($lat1, $lon1, $lat2, $lon2) {
global $earth_radius;
global $delta_lat;
global $delta_lon;
$alpha = $delta_lat/2;
$beta = $delta_lon/2;
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c = asin(min(1, sqrt($a)));
$distance = 2*$earth_radius * $c;
$distance = round($distance, 4);
return $distance;
}
$hav_distance = distance_haversine($lat_1, $lon_1, $lat_2, $lon_2);
echo $hav_distance;
?>
此代码打破了显示1的结果,我似乎无法弄清楚原因。我想也许高级自定义字段插件并不是存储lat和long的最佳方式。
答案 0 :(得分:1)
你的循环中有你的功能...每次运行时你都会重新声明抛出php错误的函数(你可能有错误的php错误)
将hasrsine函数定义放在函数文件中。