如果在wp-api中自定义帖子类型的情况下如何添加对特色图像的支持?
我找到了以下代码片段,它支持wordpress普通帖子,但没有关于自定义帖子类型的支持
function init() {
add_filter( 'rest_prepare_post', [ $this, 'add_featured_image' ], 10, 2 );
}
function add_featured_image( $data, $post ) {
$sizes = [ 'thumbnail' => '', 'medium' => '', 'large' => '', 'full' => '' ];
$_data = $data->data;
$thumbnail_id = get_post_thumbnail_id( $post->ID );
foreach ( $sizes as $size => $src ) {
$sizes[ $size ] = wp_get_attachment_image_src( $thumbnail_id, $size )[0];
}
$_data['featured_image_url'] = $sizes;
$data->data = $_data;
return $data;
}
我明白了!
过滤器应该看起来像
add_filter( 'rest_prepare_my_custom_post_type_slug', [ $this, 'add_featured_image' ], 10, 2 );