【发布时间】:2017-03-03 06:16:59
【问题描述】:
我正在为 WordPress 使用 JSON-API 插件,并且我正在创建一个自定义控制器,该控制器从帖子类型返回帖子列表。我想要的输出是这样的:
[{
"id": 1,
"title": "Sample Post Title"
},
{
"id": 2,
"title": "Sample Post Title"
},
....
]
这是我的自定义控制器:
class JSON_API_Custom_Controller {
public function get_posts_type() {
global $json_api;
$posts_all = array();
$args = array( 'post_type' => 'shopping', 'posts_per_page' => -1);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$post_title = get_the_title($post_id);
$posts_all []= array(
'id' => $post_id,
'title' => $post_title
);
endwhile;
}
return $posts_all;
}
}
但输出是:
{
"status":"ok",
"0":{
"id":1,
"title":"sample title"
},
"1":{
"id":2,
"title":"sample title"
}
}
我怎样才能像期望的输出一样输出对象的 json 数组?
【问题讨论】:
-
哇,好像投反对票的家伙在这里毫无理由地投反对票!