set_post_thumbnail()
云策文档标注
概述
set_post_thumbnail() 函数用于为指定文章设置缩略图(特色图像)。它接受文章ID或文章对象以及缩略图ID作为参数,通过更新或删除_postmeta表中的_thumbnail_id元数据来实现功能。
关键要点
- 参数:$post(必需,文章ID或WP_Post对象),$thumbnail_id(必需,缩略图附件ID)
- 返回值:成功时返回元数据ID(首次设置)或true(更新成功),失败或值未变时返回false
- 内部逻辑:验证文章和缩略图存在后,调用update_post_meta()或delete_post_meta()操作_thumbnail_id元字段
- 相关函数:包括wp_get_attachment_image()、update_post_meta()、delete_post_meta()、absint()、get_post()等
- 使用场景:被WP_REST_Posts_Controller、AJAX处理函数、wp_insert_post()等核心功能调用
代码示例
// 示例:将上传的图像文件设置为文章缩略图
$upload_file = wp_upload_bits($filename, null, @file_get_contents($file));
if (!$upload_file['error']) {
$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'
);
$attachment_id = wp_insert_attachment($attachment, $upload_file['file'], $post_id);
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$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);
}
}注意事项
- 返回值说明:成功时可能返回元数据ID或true,具体取决于是否为首次设置;失败或值未变时返回false
- 重复调用:如果缩略图已设置为相同附件ID,函数会返回false,因为update_post_meta()在值未改变时返回false
- 验证:函数内部会检查文章和缩略图是否存在,以及缩略图是否有效图像
原文内容
Sets the post thumbnail (featured image) for the given post.
Parameters
$postint|WP_Postrequired-
Post ID or post object where thumbnail should be attached.
$thumbnail_idintrequired-
Thumbnail to attach.
Source
function set_post_thumbnail( $post, $thumbnail_id ) {
$post = get_post( $post );
$thumbnail_id = absint( $thumbnail_id );
if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) {
return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
} else {
return delete_post_meta( $post->ID, '_thumbnail_id' );
}
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
Skip to note 4 content
Aurovrata Venet
To programmatically setup an uploaded image file as a thumbnail, you can use the following code…
/* * $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 ) ); i f ( ! $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' ); $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'); $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 ); } }Skip to note 5 content
John Dorner
The return value is not “True on success”
On success, the return value is the new meta field ID returned by the update_post_meta function or TRUE if delete_post_meta is successful.
Skip to note 6 content
Corey Salzano
This method will return false the second time you run it. If the featured image is already set to the attachment ID you provide, the method returns false because `update_post_meta` returns false when the value would not be changed.