使用该功能set_post_thumbnail
,
//$file is the path to your uploaded file (for example as set in the $_FILE posted file array)
//$filename is the name of the file
//first we need to upload the file into the wp upload folder.
$upload_file = wp_upload_bits($filename, null, @file_get_contents($file));
if(!$upload_file[\'error\']) {
//if succesfull insert the new file into the media library (create a new attachment post type)
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_parent\' => $post_id,
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
//wp_insert_attachment( $attachment, $filename, $parent_post_id );
$attachment_id = wp_insert_attachment( $attachment, $upload_file[\'file\'], $post_id );
if (!is_wp_error($attachment_id)) {
//if attachment post was successfully created, insert it as a thumbnail to the post $post_id
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
//wp_generate_attachment_metadata( $attachment_id, $file ); for images
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file[\'file\'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $post_id, $attachment_id );
}
}