delete_post_thumbnail()
云策文档标注
概述
delete_post_thumbnail() 函数用于从指定文章中移除特色图像(缩略图)。它通过删除文章元数据中的 _thumbnail_id 字段来实现此功能,并返回操作成功与否的布尔值。
关键要点
- 参数 $post 接受文章 ID 或 WP_Post 对象,为必填项。
- 函数返回布尔值:成功时返回 true,失败时返回 false。
- 内部调用 delete_post_meta() 来删除 _thumbnail_id 元数据。
- 自 WordPress 3.3.0 版本引入。
代码示例
function delete_post_thumbnail( $post ) {
$post = get_post( $post );
if ( $post ) {
return delete_post_meta( $post->ID, '_thumbnail_id' );
}
return false;
}注意事项
- 确保传入有效的文章 ID 或 WP_Post 对象,否则函数可能返回 false。
- 此函数仅移除特色图像关联,不会删除实际的媒体文件。
原文内容
Removes the thumbnail (featured image) from the given post.
Parameters
$postint|WP_Postrequired-
Post ID or post object from which the thumbnail should be removed.
Source
function delete_post_thumbnail( $post ) {
$post = get_post( $post );
if ( $post ) {
return delete_post_meta( $post->ID, '_thumbnail_id' );
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |