set_post_thumbnail()给文章添加自定义缩略图(特色图像)

目录

描述

Sets a post thumbnail.

set_post_thumbnail() 描述

用法

<?php set_post_thumbnail$post$thumbnail_id ); ?>

set_post_thumbnail() 用法

参数

$post

(int/object) (必填) Post ID or object where thumbnail should be attached.

默认值: None

$thumbnail_id

(integer) (必填) Thumbnail to attach.

默认值: None

set_post_thumbnail() 参数

返回值

(int/bool

Post meta ID on success, false on failure.

set_post_thumbnail() 返回值

历史

  • 添加于 版本: 3.1

set_post_thumbnail() 历史

源文件

set_post_thumbnail() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Set a post thumbnail.
 *
 * @since 3.1.0
 *
 * @param int|WP_Post $post         Post ID or post object where thumbnail should be attached.
 * @param int         $thumbnail_id Thumbnail to attach.
 * @return int|bool True on success, false on failure.
 */
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;
}

set_post_thumbnail() 源文件

相关