Php 如何在wordpress中以编程方式显示自定义文章类型的图像缩略图。?
我目前正在写一篇word press自定义文章,我从mysql数据库中获取内容,并使用wp_insert_post以编程方式创建文章。完成此操作后,标题和描述将按预期获取和显示。但是,图像不会显示,而仅打印url 请查找与图像显示相关的代码(我不确定是否需要添加任何其他标记) 请查找与获取和显示逻辑相关的完整代码Php 如何在wordpress中以编程方式显示自定义文章类型的图像缩略图。?,php,mysql,wordpress,plugins,custom-post-type,Php,Mysql,Wordpress,Plugins,Custom Post Type,我目前正在写一篇word press自定义文章,我从mysql数据库中获取内容,并使用wp_insert_post以编程方式创建文章。完成此操作后,标题和描述将按预期获取和显示。但是,图像不会显示,而仅打印url 请查找与图像显示相关的代码(我不确定是否需要添加任何其他标记) 请查找与获取和显示逻辑相关的完整代码 function techiepress_query_garage_table( $car_available_in_cpt_array ) { global $
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.'" />' ),
'post_content' => wp_strip_all_tags($result->descriptions.' <img src="'.$result->image.'" />' ),
....
$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 );
}