如何在 wordpress 中以编程方式显示自定义帖子类型的图像缩略图。?
Posted
技术标签:
【中文标题】如何在 wordpress 中以编程方式显示自定义帖子类型的图像缩略图。?【英文标题】:How to display a image thumbnail for custom post type programattically in wordpress.? 【发布时间】:2021-04-24 13:49:27 【问题描述】:我目前正在开发一个 wordpress 自定义帖子,我从 mysql 数据库中获取内容并使用 wp_insert_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 );
【问题讨论】:
【参考方案1】:它不是超级干净,但是这种技巧可以做到吗?
'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 );
【讨论】:
以上是关于如何在 wordpress 中以编程方式显示自定义帖子类型的图像缩略图。?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 中以编程方式从 UIView 中删除安全区域?
如何在 Swift 中以编程方式更改自定义 UITableViewCell 中按钮的背景颜色?