最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to set a featured image (thumbnail) with PHP?

programmeradmin2浏览0评论

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
  • You mean to set the featured image? – Tom J Nowell Commented Jun 18, 2016 at 3:32
  • @TomJNowell Yes, exactly ! – Picki Commented Jun 18, 2016 at 3:48
  • Would this help? – cjbj Commented Jun 18, 2016 at 8:37
  • Featured images are posts of type attachment, aka what you see listed in the media section of the admin area. Each post has a custom field/post meta which contains the post ID of the featured image. You will want your images as attachments so that they can be used by WordPress, rather than arbitrary files in a folder. Hopefully that information will be useful, and forms the foundation behind any answers that will be here – Tom J Nowell Commented Jun 18, 2016 at 13:23
  • I'd recommend that you update your question to show the code that creates the posts and figures out the path for the image. The answer will involve sideloading the image, probably using media_handle_sideload – Tom J Nowell Commented Jun 18, 2016 at 13:26
 |  Show 1 more comment

1 Answer 1

Reset to default 2

I 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 );
发布评论

评论列表(0)

  1. 暂无评论