has_post_thumbnail

函式
has_post_thumbnail ( $post = null )
引數
  • (int|WP_Post) $post Optional. Post ID or WP_Post object. Default is global `$post`.
    Required:
    Default: null
返回值
  • (bool) Whether the post has an image attached.
定義位置
相關方法
the_post_thumbnailset_post_thumbnailthe_post_thumbnail_urlget_the_post_thumbnailset_post_thumbnail_size
引入
2.9.0
棄用
-

has_post_thumbnail – 這是一個WordPress函式,用於檢查當前文章是否有一個特色圖片(也被稱為文章縮圖)分配給它。特色圖片被用來在各種情況下代表文章或頁面的內容,例如在搜尋結果或社交媒體上。has_post_thumbnail函式接受一個引數,即文章的ID或物件,如果文章有一個特色圖片分配給它,則返回true。

確定一個文章是否有一個圖片附件。

關於這個和類似的主題功能的更多資訊,請檢視《主題開發者手冊》中的{@link Conditional Tags}文章。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function has_post_thumbnail( $post = null ) {
$thumbnail_id = get_post_thumbnail_id( $post );
$has_thumbnail = (bool) $thumbnail_id;
/**
* Filters whether a post has a post thumbnail.
*
* @since 5.1.0
*
* @param bool $has_thumbnail true if the post has a post thumbnail, otherwise false.
* @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`.
* @param int|false $thumbnail_id Post thumbnail ID or false if the post does not exist.
*/
return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
}
function has_post_thumbnail( $post = null ) { $thumbnail_id = get_post_thumbnail_id( $post ); $has_thumbnail = (bool) $thumbnail_id; /** * Filters whether a post has a post thumbnail. * * @since 5.1.0 * * @param bool $has_thumbnail true if the post has a post thumbnail, otherwise false. * @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`. * @param int|false $thumbnail_id Post thumbnail ID or false if the post does not exist. */ return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id ); }
function has_post_thumbnail( $post = null ) {
	$thumbnail_id  = get_post_thumbnail_id( $post );
	$has_thumbnail = (bool) $thumbnail_id;

	/**
	 * Filters whether a post has a post thumbnail.
	 *
	 * @since 5.1.0
	 *
	 * @param bool             $has_thumbnail true if the post has a post thumbnail, otherwise false.
	 * @param int|WP_Post|null $post          Post ID or WP_Post object. Default is global `$post`.
	 * @param int|false        $thumbnail_id  Post thumbnail ID or false if the post does not exist.
	 */
	return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
}

常見問題

FAQs
檢視更多 >