set_post_thumbnail() | WordPress设置文章特色图

作者猫斯基 2015-03-01 263 人读过

最新文章

函数

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
Uses Description
wp-includes/functions.php:
absint()
wp-includes/media.php:
wp_get_attachment_image()
wp-includes/post.php:
update_post_meta()
wp-includes/post.php:
delete_post_meta()
wp-includes/post.php:
get_post()
Used By
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()
wp-admin/includes/ajax-actions.php:
wp_ajax_set_post_thumbnail()
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/2904.html

关注我们

站长

WordPress迷