一般来说,我对Wordpress & Php编码非常陌生,我有一个相对简单的主题,我一直在处理这个主题,它包含一个存储纬度和经度的定制post类型。每个帖子都是一个在目录中列出的酒店列表。我所使用的主题还集成了FacetWP,这是一个面向WordPress的插件。用户可以通过键入自己的位置来搜索站点,这是使用Google映射功能实现的。当搜索被提交时,参数被传递到存档页面:
sort=distance
破译后的结果如下:
43.653226,-79.383184299998,10,多伦多%2C%20,加拿大%2C%
因此,表单在半径(10英里)处通过了档案馆的中心纬度和经度。
然后,归档文件使用这些信息查询post类型列表,以返回附近的位置。
我真正想做的是在归档/ post概览页面中显示每个帖子与中心位置的距离。
这应该是可能的,因为主题有一个按距离排序的选项。我只是想不出如何获取计算出来的值并将其回显在页面正文中。邻近函数的php是:
<?php
if ( class_exists( 'FacetWP_Facet_Proximity' ) ) {
return;
}
class FacetWP_Facet_Proximity
{
/**
* The ordered array of post IDs
*/
public $ordered_posts = array();
/**
* An array containing each post ID and its distance
*/
public $distance = array();
function __construct() {
$this->label = __( 'Proximity', 'fwp' );
add_filter( 'facetwp_index_row', array( $this, 'index_latlng' ), 10, 2 );
add_filter( 'facetwp_sort_options', array( $this, 'sort_options' ), 1, 2 );
add_filter( 'facetwp_filtered_post_ids', array( $this, 'sort_by_distance' ), 10, 2 );
}
/**
* Generate the facet HTML
*/
function render( $params ) {
$output = '';
$facet = $params['facet'];
$value = $params['selected_values'];
$unit = empty( $facet['unit'] ) ? 'mi' : $facet['unit'];
$lat = empty( $value[0] ) ? '' : $value[0];
$lng = empty( $value[1] ) ? '' : $value[1];
$chosen_radius = empty( $value[2] ) ? '' : $value[2];
$location_name = empty( $value[3] ) ? '' : urldecode( $value[3] );
$radius_options = apply_filters( 'facetwp_proximity_radius_options', array( 10, 25, 50, 100, 250 ) );
ob_start(); ?>
<input type="text" id="facetwp-location" value="<?php echo $location_name; ?>" placeholder="<?php _e( 'Enter location', 'fwp' ); ?>" />
<select id="facetwp-radius">
<?php foreach ( $radius_options as $radius ) : ?>
<?php $selected = ( $chosen_radius == $radius ) ? ' selected' : ''; ?>
<option value="<?php echo $radius; ?>"<?php echo $selected; ?>><?php echo "$radius $unit"; ?></option>
<?php endforeach; ?>
</select>
<div style="display:none">
<input type="text" class="facetwp-lat" value="<?php echo $lat; ?>" />
<input type="text" class="facetwp-lng" value="<?php echo $lng; ?>" />
</div>
<?php
return ob_get_clean();
}
/**
* Filter the query based on selected values
*/
function filter_posts( $params ) {
global $wpdb;
$facet = $params['facet'];
$selected_values = $params['selected_values'];
$unit = empty( $facet['unit'] ) ? 'mi' : $facet['unit'];
$earth_radius = ( 'mi' == $unit ) ? 3959 : 6371;
if ( empty( $selected_values ) || empty( $selected_values[0] ) ) {
return 'continue';
}
$lat = (float) $selected_values[0];
$lng = (float) $selected_values[1];
$radius = (int) $selected_values[2];
$sql = "
SELECT DISTINCT post_id,
( $earth_radius * acos( cos( radians( $lat ) ) * cos( radians( facet_value ) ) * cos( radians( facet_display_value ) - radians( $lng ) ) + sin( radians( $lat ) ) * sin( radians( facet_value ) ) ) ) AS distance
FROM {$wpdb->prefix}facetwp_index
WHERE facet_name = '{$facet['name']}'
HAVING distance < $radius
ORDER BY distance";
$this->ordered_posts = array();
$this->distance = array();
if ( apply_filters( 'facetwp_proximity_store_distance', false ) ) {
$results = $wpdb->get_results( $sql );
foreach ( $results as $row ) {
$this->ordered_posts[] = $row->post_id;
$this->distance[ $row->post_id ] = $row->distance;
}
}
else {
$this->ordered_posts = $wpdb->get_col( $sql );
}
return $this->ordered_posts;
}您所能提供的任何帮助都将不胜感激,我已经尝试过阅读wpquery并对其进行了实验,但似乎并不适合,因为距离不是存储的值,而是一种计算。
发布于 2015-11-01 16:26:39
我从Matt@wordpress.stackexchange收到并回答了我的问题。实际上,有一个FacetWP钩子可以向页面模板添加距离。功能过滤器:
add_filter( 'facetwp_proximity_store_distance', '__return_true' );和一些php来启用以下功能:
$distance = facetwp_get_distance();
if ( false !== $distance ) {
echo round( $distance, 1 );
echo " miles";
}我希望这能帮上忙,再次感谢马特
https://stackoverflow.com/questions/33403124
复制相似问题