返回介绍

set_post_thumbnail()

发布于 2017-09-11 10:22:01 字数 4107 浏览 1340 评论 0 收藏 0

set_post_thumbnail( int|WP_Post $post,  int $thumbnail_id )

Set a post thumbnail.


description


参数

$post

(int|WP_Post) (Required) Post ID or post object where thumbnail should be attached.

$thumbnail_id

(int) (Required) Thumbnail to attach.


返回值

(int|bool) True on success, false on failure.


源代码

File: 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;
}

更新日志

Versiondescription
3.1.0Introduced.

相关函数

Uses

  • 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

  • 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()
  • Show 2 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by 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 );
       }
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文