WP REST API 自定义端点如何 return 自定义 post 数据?
How WP REST API custom endpoint could return custom post data?
TL;DR: 如何选择 WP REST API 自定义端点响应的每一位信息?
长版
如果我想使用 WP REST API 构建自定义端点 - 发送来自不同 post 类型的特定 post 数据 - 按照 Handbook 中的示例,我得到了这个:
function custom_endpoint ( $data ) {
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => array('event', 'post'),
) );
if ( empty( $posts ) ) {
return null;
}
return $posts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v1', '/custom-endpoint/', array(
'methods' => 'GET',
'callback' => 'custom_endpoint',
) );
} );
但是 get_post() 函数没有 return 一些非常有用的数据,如果你想在你的页面中显示 posts(类别 id,精选图像,例如)。那么我如何构建一个 returns:
的自定义端点
- Post 标题
- Post 日期
- Post作者
- Post 摘录
- Post内容
- Post 特色图片(如 Better Featured Images plugin)
- Post 类别
- Post类型
- Post Link
- 其他有用信息
作为WP Codex states访问所有数据:
访问所有 post 数据 某些 post-related 数据 不可用 到 get_posts。
您可以通过以下方式获得它们:
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => array('event', 'post'),
) );
$response = [];
foreach ( $posts as $post ) {
$response[] = [
'content' => $post->CONTENT.
'title' => $post->TITLE,
.....
]
}
return $response; (in a WP way of constructing json responses)
根据@fsn 的回答,我得到以下想法:使用其他 Wordpress 函数获取 get_posts() returns 的对象并向其添加新的比例。
function custom_endpoint ( $data ) {
$posts = get_posts( array(
'numberposts' => -1,
//Here we can get more than one post type. Useful to a home page.
'post_type' => array('event', 'post'),
) );
if ( empty( $posts ) ) {
return null;
}
$args = array();
foreach ( $posts as $post ) {
//Get informations that is not avaible in get_post() function and store it in variables.
$category = get_the_category( $post->ID );
$img_thumb = get_the_post_thumbnail_url( $post->ID, 'thumbnail' ); // Thumbnail (default 150px x 150px max)
$img_medium = get_the_post_thumbnail_url( $post->ID, 'medium' ); // Medium resolution (default 300px x 300px max)
$img_large = get_the_post_thumbnail_url( $post->ID, 'large' ); // Large resolution (default 640px x 640px max)
$img_full = get_the_post_thumbnail_url( $post->ID, 'full' ); // Full resolution (original size uploaded)
//Adds the informations to the post object.
$post->category = $category;
$post->img_tumb = $img_thumb;
$post->img_medium = $img_medium;
$post->img_large = $img_large;
$post->img_full = $img_full;
array_push($args, $post);
}
return $args;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v1', '/custom-endpoint/', array(
'methods' => 'GET',
'callback' => 'custom_endpoint',
) );
});
工作正常!
感谢@fsn 的贡献。
TL;DR: 如何选择 WP REST API 自定义端点响应的每一位信息?
长版
如果我想使用 WP REST API 构建自定义端点 - 发送来自不同 post 类型的特定 post 数据 - 按照 Handbook 中的示例,我得到了这个:
function custom_endpoint ( $data ) {
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => array('event', 'post'),
) );
if ( empty( $posts ) ) {
return null;
}
return $posts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v1', '/custom-endpoint/', array(
'methods' => 'GET',
'callback' => 'custom_endpoint',
) );
} );
但是 get_post() 函数没有 return 一些非常有用的数据,如果你想在你的页面中显示 posts(类别 id,精选图像,例如)。那么我如何构建一个 returns:
的自定义端点- Post 标题
- Post 日期
- Post作者
- Post 摘录
- Post内容
- Post 特色图片(如 Better Featured Images plugin)
- Post 类别
- Post类型
- Post Link
- 其他有用信息
作为WP Codex states访问所有数据:
访问所有 post 数据 某些 post-related 数据 不可用 到 get_posts。
您可以通过以下方式获得它们:
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => array('event', 'post'),
) );
$response = [];
foreach ( $posts as $post ) {
$response[] = [
'content' => $post->CONTENT.
'title' => $post->TITLE,
.....
]
}
return $response; (in a WP way of constructing json responses)
根据@fsn 的回答,我得到以下想法:使用其他 Wordpress 函数获取 get_posts() returns 的对象并向其添加新的比例。
function custom_endpoint ( $data ) {
$posts = get_posts( array(
'numberposts' => -1,
//Here we can get more than one post type. Useful to a home page.
'post_type' => array('event', 'post'),
) );
if ( empty( $posts ) ) {
return null;
}
$args = array();
foreach ( $posts as $post ) {
//Get informations that is not avaible in get_post() function and store it in variables.
$category = get_the_category( $post->ID );
$img_thumb = get_the_post_thumbnail_url( $post->ID, 'thumbnail' ); // Thumbnail (default 150px x 150px max)
$img_medium = get_the_post_thumbnail_url( $post->ID, 'medium' ); // Medium resolution (default 300px x 300px max)
$img_large = get_the_post_thumbnail_url( $post->ID, 'large' ); // Large resolution (default 640px x 640px max)
$img_full = get_the_post_thumbnail_url( $post->ID, 'full' ); // Full resolution (original size uploaded)
//Adds the informations to the post object.
$post->category = $category;
$post->img_tumb = $img_thumb;
$post->img_medium = $img_medium;
$post->img_large = $img_large;
$post->img_full = $img_full;
array_push($args, $post);
}
return $args;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v1', '/custom-endpoint/', array(
'methods' => 'GET',
'callback' => 'custom_endpoint',
) );
});
工作正常!
感谢@fsn 的贡献。