get_post_feature_image($post_id) 不起作用

问题描述 投票:0回答:1

我是使用 WordPress 的新手,我不经常使用 php,而且我没有太多时间来了解这一点,所以请如果有人知道是什么导致了这里的问题,我将非常感激这里的functions.php代码:

function get_post_feature_image($post_id){
    $args = array(
        'posts_per_page'=>1,
        'order'=>'asc',
        'post_mime_type'=>'image',
        'post_parent'=>$post_id,
        'post_type'=>'attachment',
        );

$attachments = get_children($args);
return wp_get_attachment_image_src(array_values($attachments)[0]->ID, 'app-thumb')[0];

}

并在此处调用它来获取帖子图像:

function get_post_by_id($request) { 
    $post = get_post($request["id"]);
    $post_categories = wp_get_post_categories($post->ID);
    $post_output = (object)array(
        "id"=> $post->ID,
        "post_date"=> $post->post_date,
        "title"=>$post->post_title,
        "category_name"=>get_category($post_categories[0])->name, 
        "post_content"=>$post->post_content,
        "imageurl"=>get_post_feature_image($post->ID) // <== here 
    );

    $response = new WP_REST_Response($post_output);
    $response->set_status(200);
    
    return $response;
    
}

heres the 1st element of the response when calling latest-posts?category_id=33

[这是调用 post-details 时的响应?id=5692] (https://i.stack.imgur.com/VuExB.png)

它不会在第二个响应中返回任何图像,但在第一个响应中它会返回某种形式的广告禁止内容

我不知道该尝试什么,因为我到处搜索并且该功能“应该”工作但它没有工作

php wordpress api rest wordpress-rest-api
1个回答
0
投票

嘿,您没有使用正确的功能:

用这个代替:

get_the_post_thumbnail_url($post->ID)

© www.soinside.com 2019 - 2024. All rights reserved.