如何使用JSON api仅获取帖子标题,我的WordPress博客的所有帖子摘录。
目前,我使用的是https://www.example.com/wp-json/wp/v2/posts,它返回的大量数据减慢了整个过程。有没有什么网址可以让我只获取选定的字段?
发布于 2017-09-12 20:39:18
您是否考虑过编写自己的API端点?https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
在端点变为http://example.com/wp-json/myplugin/v1/post-titles的情况下,这样的代码可能会对您起作用
function my_awesome_func( $data ) {
$args = array(
'post_type' => 'post',
);
$query = new WP_Query( $args );
$arr = array();
while ( $query->have_posts() ) {
$query->the_post();
$titles = get_the_title();
array_push($arr, $titles);
}
return $arr;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/post-titles', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
https://stackoverflow.com/questions/46177589
复制相似问题