我没有使用带有类别(属性)的常规帖子,而是创建了一个自定义帖子类型(属性)。因此,我想转换以下代码,以便它使用自定义的post类型而不是category。
我是一个完全的新手,所以需要一些帮助:)
// For each property post, convert its ACF address fields to longitude/latitude coords and add to map
var geojson = [
<?php
$category_id = get_cat_ID(\'properties\');
$catquery = new WP_Query( \'cat=\' .$category_id. \'&posts_per_page=100\' );
while($catquery->have_posts()) : $catquery->the_post();
$latitude = get_post_meta($post->ID, \'latitude\', true);
$longitude = get_post_meta($post->ID, \'longitude\', true);
if ($latitude != \'\' ) {} else { $latitude = 0; }
if ($longitude != \'\' ) {} else { $longitude = 0; }
$permalink = get_permalink();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ($feat_image != \'\' ){} else { $feat_image = get_stylesheet_directory_uri() . "/_public/comingsoon_blank.jpg"; }
?>
{
"type": "Feature",
"geometry": {
"coordinates": ["<?php echo $longitude ?>", "<?php echo $latitude ?>"],
"type": "Point"
},
"properties": {
"title": "<?php the_field(\'property_address_line_1\'); ?>",
"neighborhood": "<?php the_field(\'property_neighborhood\'); ?>",
"photo": "<?php echo $feat_image ?>",
"permalink": "<?php echo $permalink ?>",
"marker-color": "#000000",
"marker-size": "large"
}
},
具有类别(Properties)的常规帖子具有property\\u address\\u line\\u 1和property\\u address\\u line\\u 2的ACF字段。我完全复制了它们,以便自定义帖子类型(Property)也有Property\\u address\\u line\\u 1和Property\\u address\\u line\\u 2的ACF字段。
在我的功能中。php文件的代码如下:
function geocode_address($post_id)
{
$resp = wp_remote_get( "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode( get_field(\'property_address_line_1\') . \' \' . get_field(\'property_address_line_2\') )."&sensor=false" );
if ( 200 == $resp[\'response\'][\'code\'] ) {
$body = $resp[\'body\'];
$data = json_decode($body);
if($data->status=="OK"){
$latitude = $data->results[0]->geometry->location->lat;
$longitude = $data->results[0]->geometry->location->lng;
update_post_meta($post_id, "latitude", $latitude);
update_post_meta($post_id, "longitude", $longitude);
}
}
}
add_action(\'save_post\', \'geocode_address\');
对于具有类别的常规帖子,一切都很好。我只想让它现在与我创建的自定义帖子类型一起工作(将地址转换为lat/long并在地图上显示)。
提前感谢!