[英]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.