Php 为什么get_页面返回错误的数据而不是我想要的?
我正在构建一个函数,它使用get_页面构建一个数组,然后将该数组转换为json 一切正常,但是查询得到的是开发中的所有帖子,而不是当前查看的开发中的唯一子页面 我尝试过使用WP_查询,但是在后端运行时会出现问题Php 为什么get_页面返回错误的数据而不是我想要的?,php,wordpress,Php,Wordpress,我正在构建一个函数,它使用get_页面构建一个数组,然后将该数组转换为json 一切正常,但是查询得到的是开发中的所有帖子,而不是当前查看的开发中的唯一子页面 我尝试过使用WP_查询,但是在后端运行时会出现问题 function data_feed() { $i = 0; $map_builder = array(); $args = array( 'post_type'=> 'developments', 'child_of
function data_feed() {
$i = 0;
$map_builder = array();
$args = array(
'post_type'=> 'developments',
'child_of' => $post->ID,
);
$the_query = get_pages($args);
foreach ( $the_query as $post_f ) : setup_postdata( $post );
if($i == 0){ $lat = 50; $long = 50; } else { $lat = ''; $long = ''; }
$map_builder[$i] = array(
'title' => get_the_title($post->ID),
'excerpt' => get_the_excerpt($post->ID),
'price' => '9999999',
'status' => 'available',
'coords' => array( 'lat' => $lat, 'long' => $long )
);
$i++;
endforeach;
wp_reset_postdata();
$jqa = array('map-pin'=> $map_builder + array( 'canvas' => array( 'src' => 'http://localhost/acf-property-manager-app/wp-content/uploads/2018/12/the-avenues-site-plan-new.png', 'width'=> 1000, 'height' => 562 )));
return json_encode( $jqa );
}
现在它返回所有正在开发中的帖子
它应该返回的只是当前查看开发的子帖子。添加:-
global $post;
将正确的ID传递给查询,结果现在是正确的
'child\u of'=>$post->ID,
-在哪里定义了$post
?在你尝试使用它之前,请先看看这个变量是什么。啊$post->ID应该返回页面的ID,但它需要全局$post;补充。真不敢相信我错过了,多谢!