我目前正在处理一个单词印刷自定义帖子,我从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 );
}
}
这不是超级干净,但是这种技巧能起作用吗?
'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 );
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句