define(\'API_ENDPOINT_VERSION\', 1);
//flush the rewrite rules on plugin activation
function apiendpoint_activate() {
flush_rewrite_rules(); } register_activation_hook(__FILE__, \'apiendpoint_activate\');
function apiendpoint_register_endpoints() {
register_rest_route(
\'api/v1\',
\'/post\',
[
\'methods\' => \'GET\',
\'callback\' => \'api_get_post\',
]
);
} add_action(\'rest_api_init\', \'apiendpoint_register_endpoints\');
function api_get_post($request) {
$ar = array(\'post_type\' => \'posts\',
\'posts_per_page\' => 15,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'paged\' => ($_REQUEST[\'paged\'] ? $_REQUEST[\'paged\'] : 1)
);
$posts = get_posts($ar); //var_dump($posts); //exit; $a = array();
if ($posts) {
foreach($posts as $post) {
$a[] = array(
\'title\' => $post -> post_title,
\'link\' => get_the_permalink($post -> ID),
\'category\' => get_the_category($post -> ID),
\'published_date\' => get_the_date(\'l, F j, Y\', $post -> ID),
\'guid\' => $post -> guid,
\'image\' => get_the_post_thumbnail_url($post -> ID, \'large\'),
\'description\' => $post -> post_excerpt,
\'source\' => "Nepaljapan"
//\'img\'=>$img
);
}
return $a; }
}
调用API调用如下:
/wp json/api/v1/post?分页=1
将分页值增加1以获取下一个分页帖子。这是我们得到的正确答案,并且对我起到了作用