【发布时间】:2019-07-30 11:38:17
【问题描述】:
我需要创建一个 API,以按类别过滤器呈现相关帖子。我已经在我的 functions.php 文件中编写了代码,但我没有明白如何将帖子 ID 传递给参数?
function related_posts_endpoint( $request_data ) {
$uposts = get_posts(
array(
'post_type' => 'post',
'category__in' => wp_get_post_categories(183),
'posts_per_page' => 5,
'post__not_in' => array(183),
) );
return $uposts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'sections/v1', '/post/related/', array(
'methods' => 'GET',
'callback' => 'related_posts_endpoint'
) );
} );
我需要从我当前的 API 调用中传递 id。因此,我需要将该 id 传递给我当前作为静态 (180) 传递的相关 API 参数
【问题讨论】:
标签: wordpress wordpress-rest-api