我正在使用JSON-API插件为WordPress和我正在创建一个自定义控制器,返回帖子类型的帖子列表。我想输出是这样的:为WordPress帖子输出JSON
[{
"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数组像所需的输出?
哇似乎下来投票人在这里下来投票没有理由的一切! –