set_post_thumbnail()
set_post_thumbnail( int|WP_Post $post, int $thumbnail_i…
set_post_thumbnail( int|WP_Post $post, int $thumbnail_id )
设置给定文章的文章缩略图(特色图像)。
Sets the post thumbnail (featured image) for the given post.
设置给定文章的文章缩略图(特色图像)。
Sets the post thumbnail (featured image) for the given post.
目录锚点:#参数#源码#笔记
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $post | (int | WP_Post) | 应附加缩略图的Post ID或Post对象。 |
| $thumbnail_id | (int) | 要附加的缩略图。 |
源码(Source)
/**
* 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;
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 3.1.0 | wp-includes/post.php | 13 | 13 |
笔记(Notes)
要以编程方式将上载的图像文件设置为缩略图,可以使用以下代码…
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!