如何在 wordpress 中以编程方式显示自定义 post 类型的图像缩略图。?

How to display a image thumbnail for custom post type programattically in wordpress.?

我目前正在开发 word press 自定义 post,我从 mysql 数据库中获取内容并使用 wp_insert_post.After 以编程方式创建 post 并执行此标题说明已按预期获取和显示。然而,图像并未显示,而是 url 单独打印。

请找到我的图像显示相关的代码(我不确定是否需要添加其他标签)。

'post_content' => wp_strip_all_tags($result->descriptions.''.$result->image ),

请找到与获取和显示逻辑相关的完整代码。

       function techiepress_query_garage_table( $car_available_in_cpt_array ) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'garage';

    if ( NULL === $car_available_in_cpt_array || 0 === $car_available_in_cpt_array || '0' === $car_available_in_cpt_array || empty( $car_available_in_cpt_array ) ) {
        $results = $wpdb->get_results("SELECT * FROM $table_name");
        return $results;
    } else {
        $ids = implode( ",", $car_available_in_cpt_array );
        $sql = "SELECT * FROM $table_name WHERE id NOT IN ( $ids )";
        $results = $wpdb->get_results( $sql );
        return $results;
    }
}

function techiepress_insert_into_auto_cpt() {

    $car_available_in_cpt_array = techiepress_check_for_similar_meta_ids();
    $database_results = techiepress_query_garage_table( $car_available_in_cpt_array );

    if ( NULL === $database_results || 0 === $database_results || '0' === $database_results || empty( $database_results ) ) {
        return;
    }

    foreach ( $database_results as $result ) {
        $car_model = array(
            'post_title' => wp_strip_all_tags( $result->Brand),
            'post_content' => wp_strip_all_tags($result->descriptions.''.$result->image ),
            
            'meta_input' => array(
                'id'        => $result->id,
                'brand'        => $result->Brand,
                'model'        => $result->Model,
                'color'        => $result->Color,
                'km'           => $result->Km,
            ),
            'post_type'   => 'auto',
            'post_status' => 'publish',
            
            
            
        );
        wp_insert_post( $car_model );
    }
} 

它不是很干净,但这种技巧可以做到吗?

'post_content' => wp_strip_all_tags($result->descriptions.' <img src="'.$result->image.'" />' ),

否则按照 WordPress 逻辑

     ....
    $postId = wp_insert_post( $car_model );
    if(!empty($result->image)){
    $upload = wp_upload_bits('My car picture' , null, file_get_contents($result->image, FILE_USE_INCLUDE_PATH));
    // check and return file type
    $imageFile = $upload['file'];
    $wpFileType = wp_check_filetype($imageFile, null);
    // Attachment attributes for file
    $attachment = array(
        'post_mime_type' => $wpFileType['type'],  // file type
        'post_title' => sanitize_file_name($imageFile),  // sanitize and use image name as file name
        'post_content' => '',  // could use the image description here as the content
        'post_status' => 'inherit'
    );
    // insert and return attachment id
    $attachmentId = wp_insert_attachment( $attachment, $imageFile, $postId );
    // insert and return attachment metadata
    $attachmentData = wp_generate_attachment_metadata( $attachmentId, $imageFile);
    // update and return attachment metadata
    wp_update_attachment_metadata( $attachmentId, $attachmentData );
    set_post_thumbnail( $postId, $attachmentId );
    }