[英]How to return data in loop for WP REST API?
function getProgramTourInfoByID(){
$data = getProgramTourInfoByCate();
//print_r($data);
foreach ($data as $key => $id) {
//echo( $id);
$count =0;
while( have_rows('package_scd', $id ) ): the_row();
//if(get_sub_field('start_date') != null){
//echo $id."\r\n";
$array[$count] = array(
'id' => $id ,
'title' => get_the_title($id),
'url' => get_the_permalink($id),
'start' => get_sub_field('start_date'),
'end' => get_sub_field('end_date'),
'price_single_room' => get_sub_field('price_single_room'),
'price_adult' => get_sub_field('price_adult'),
'type' => "restricted",
);
$count ++;
endwhile;
}
$output = array('sourcename' => $array );
return $output;
}
function getProgramTourInfoByCate(){
$term_id = 3;
$args = array(
'post_type' => array( 'package'),
'posts_per_page' => -1,
'post_status' => array('publish') ,
'tax_query' => array(
array(
'taxonomy' => 'tour-categories',
'field' => 'id',
'terms' => $term_id,
'order'=>'DESC',
'orderby'=>'menu_order',
)
)
);
$query = new WP_Query($args);
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
$arr[] = get_the_ID();
endwhile;
return $arr;
endif;
}
这是我的代码,但是返回错误的输出。 当我在循环中测试echo $id
while( have_rows('package_scd', $id ) ): the_row()
,从数据库返回所有ID,这是正确的输出:
但是当我返回$output = array('sourcename' => $array )
。 仅返回两个ID(24930和18367),我不确定为什么其余ID不来。
可能是它覆盖了$ array键。
$count = 0;
$array = array();
在foreach循环开始之前。 我敢肯定,这会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.