admin管理员组文章数量:1334188
I am creating posts using wp_insert_post()
, but these posts do not have featured images.
I have a post ID, and a path to the image on my server, how do I set them as the featured image?
EDIT : I added my code to publish an article. It only needs to add the thumbnail (featured image).
imagePath= ABSPATH . "wp-content\uploads\image\my_image.jpg";
publication($titre, $content, $categriesID, $tags);
function publication ($titre, $content, $categories, $tags){
$user_id = get_current_user_id();
$my_post = array(
'post_title' => $titre,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => $user_id,
'post_category' => $categories,
'tags_input' => $tags
);
$post_ID = wp_insert_post( $my_post );
}
I am creating posts using wp_insert_post()
, but these posts do not have featured images.
I have a post ID, and a path to the image on my server, how do I set them as the featured image?
EDIT : I added my code to publish an article. It only needs to add the thumbnail (featured image).
imagePath= ABSPATH . "wp-content\uploads\image\my_image.jpg";
publication($titre, $content, $categriesID, $tags);
function publication ($titre, $content, $categories, $tags){
$user_id = get_current_user_id();
$my_post = array(
'post_title' => $titre,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => $user_id,
'post_category' => $categories,
'tags_input' => $tags
);
$post_ID = wp_insert_post( $my_post );
}
Share
Improve this question
edited Jun 27, 2020 at 20:51
Kalnode
2261 silver badge11 bronze badges
asked Jun 18, 2016 at 2:30
PickiPicki
1215 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 2I am proud to announce that I have found the solution thanks to @cjbj good advice. I paste the code here. I know it will help others.
In addition, I used the function preg_quote
to protect the path to the image.
protected_path = preg_quote($filename);
This code works great.
// $filename should be the path to a file in the upload directory.
$filename = '/path/to/uploads/2013/03/filename.jpg';
// The ID of the post this attachment is for.
$parent_post_id = 37;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $parent_post_id, $attach_id );
本文标签: How to set a featured image (thumbnail) with PHP
版权声明:本文标题:How to set a featured image (thumbnail) with PHP? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313142a2451281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
media_handle_sideload
– Tom J Nowell ♦ Commented Jun 18, 2016 at 13:26