set_post_thumbnail() | WordPress设置文章特色图
8
2015-03-01
函数
set_post_thumbnail( int|WP_Post $post, int $thumbnail_id )
描述
该WordPress函数可 给指定的文章设置特色图
参数
$post
(int | WP_Post) (必需) 需要添加缩略图的文章ID或文章对象。
$thumbnail_id
(int) (必需) 缩略图ID。
返回值
(int | bool)成功则为True
,失败则为false
。
猫斯基划重点
成功时,返回值是update_post_meta
函数返回的新元字段ID;如果delete_post_meta
成功,则返回TRUE
。
该方法将在您第二次运行时返回false
。
如果特色图像已经设置为您提供的附件ID,则该方法返回false
,因为在不更改值的情况下update_post_meta
返回false
。
实例
以代码方式将上传的图像文件设置为缩略图:
/* * $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 ); } }
猫斯基注解
Uses | Description |
---|---|
wp-includes/functions.php: absint() |
将值转换为非负整数 |
wp-includes/media.php: wp_get_attachment_image() |
获取表示图像附件的HTML img元素 |
wp-includes/post.php: update_post_meta() |
根据帖子ID更新文章元字段 |
wp-includes/post.php: delete_post_meta() |
删除指定文章ID的元字段。 |
wp-includes/post.php: get_post() |
根据文章ID或文章对象,获取文章数据 |
Used By | Description |
---|---|
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::handle_featured_media() |
根据请求参数确定特色图 |
wp-admin/includes/ajax-actions.php: wp_ajax_set_attachment_thumbnail() |
Ajax处理程序,用于设置附件的特色图 |
wp-admin/includes/ajax-actions.php: wp_ajax_set_post_thumbnail() |
Ajax处理程序,用于设置特色图 |
wp-includes/post.php: wp_insert_post() |
插入或更新帖子 |
wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_editPost() |
编辑帖子 |
wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_newPost() |
创建一个新帖子 |
wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_insert_post() |
|
修改记录
wordpress 3.1.0
源文件
wp-includes / post.php
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; }
本文由 猫斯基 原创发布。
著作权均归用户本人所有。独家文章转载,请联系本站管理员。获得授权后,须注明本文地址! 本文地址:https://www.maosiji.com/wordpress-set_post_thumbnail.html