WordPress: WP Rest API – 如何获取特色图片(缩略图) featured image

2022-07-21
  1. 给api后加
    _embed
    _embed
url: 'https://www.xalhsz.cn/wp-json/wp/v2/doctor?_embed'
url: 'https://www.xalhsz.cn/wp-json/wp/v2/doctor?_embed'
url: 'https://www.xalhsz.cn/wp-json/wp/v2/doctor?_embed'
{{item._embedded['wp:featuredmedia'][0].source_url}} //微信小程序中如此调用
{{item._embedded['wp:featuredmedia'][0].source_url}} //微信小程序中如此调用
{{item._embedded['wp:featuredmedia'][0].source_url}}  //微信小程序中如此调用

2. 在functions.php中加如下代码

function post_featured_image_json($data,$post,$context){
$featured_image_id = $data->data['featured_media'];
$featured_image_url = wp_get_attachment_image_src($featured_image_id,'original');
if($featured_image_url){
$data->data['featured_image_url'] = $featured_image_url[0];
}
return $data;
}
add_filter('rest_prepare_post','post_featured_image_json',10,3);
add_filter('rest_prepare_doctor','post_featured_image_json',10,3); //rest_prepare_doctor就可以获取doctor类型内容
function post_featured_image_json($data,$post,$context){ $featured_image_id = $data->data['featured_media']; $featured_image_url = wp_get_attachment_image_src($featured_image_id,'original'); if($featured_image_url){ $data->data['featured_image_url'] = $featured_image_url[0]; } return $data; } add_filter('rest_prepare_post','post_featured_image_json',10,3); add_filter('rest_prepare_doctor','post_featured_image_json',10,3); //rest_prepare_doctor就可以获取doctor类型内容
    function post_featured_image_json($data,$post,$context){
        $featured_image_id = $data->data['featured_media'];
        $featured_image_url = wp_get_attachment_image_src($featured_image_id,'original');
        if($featured_image_url){
            $data->data['featured_image_url'] = $featured_image_url[0];
        }
        return $data;
    }
    add_filter('rest_prepare_post','post_featured_image_json',10,3);
    add_filter('rest_prepare_doctor','post_featured_image_json',10,3);  //rest_prepare_doctor就可以获取doctor类型内容

3. 在functions.php中如下,还可以调用出其他字段

//Add in functions.php,this hook is for my 'doctor' post type
add_action('rest_api_init','create_api_posts_meta_field');
function create_api_posts_meta_field(){
register_rest_field('doctor','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;
}
//Add in functions.php,this hook is for my 'doctor' post type add_action('rest_api_init','create_api_posts_meta_field'); function create_api_posts_meta_field(){ register_rest_field('doctor','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; }
    //Add in functions.php,this hook is for my 'doctor' post type
    add_action('rest_api_init','create_api_posts_meta_field');
    function create_api_posts_meta_field(){
        register_rest_field('doctor','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;
    }

效果如下:

相关内容
最新

坚持的力量

wordpress建站,视频剪辑拍摄,动画制作