wpseek.com
WordPress开发者和主题制作者的搜索引擎



set_post_thumbnail › WordPress Function

Since3.1.0
已弃用n/a
set_post_thumbnail ( $post, $thumbnail_id )
参数: (2)
  • (int|WP_Post) $post Post ID or post object where thumbnail should be attached.
    Required: Yes
  • (int) $thumbnail_id Thumbnail to attach.
    Required: Yes
返回:
  • (int|bool) True on success, false on failure.
定义在:
文档:

Sets the post thumbnail (featured image) for the given post.



源码

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