set_post_thumbnail

函式
set_post_thumbnail ( $post, $thumbnail_id )
引數
  • (int|WP_Post) $post Post ID or post object where thumbnail should be attached.
    Required:
  • (int) $thumbnail_id Thumbnail to attach.
    Required:
返回值
  • (int|bool) True on success, false on failure.
定義位置
相關方法
the_post_thumbnailhas_post_thumbnailset_post_thumbnail_sizedelete_post_thumbnailget_post_thumbnail_id
引入
3.1.0
棄用
-

set_post_thumbnail: 這是一個WordPress的函式,用於設定當前文章的縮圖(也稱為特色圖片)。它通常在文章編輯器中使用,以選擇一個圖片作為文章的縮圖: 這個函式有一個引數,就是要設定為文章縮圖的圖片的ID。

為給定的文章設定文章縮圖(特色圖片)。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >