我通过将图像直接添加到API响应中,创建了图像的快捷方式。
<小时>
//Add in functions.php, this hook is for my \'regions\' post type
add_action( \'rest_api_init\', \'create_api_posts_meta_field\' );
function create_api_posts_meta_field() {
register_rest_field( \'regions\', \'group\', array(
\'get_callback\' => \'get_post_meta_for_api\',
\'schema\' => null,
)
);
}
<小时>
//Use the post ID to query the image and add it to your payload
function get_post_meta_for_api( $object ) {
$post_id = $object[\'id\'];
$post_meta = get_post_meta( $post_id );
$post_image = get_post_thumbnail_id( $post_id );
$post_meta["group_image"] = wp_get_attachment_image_src($post_image)[0];
return $post_meta;
}