WordPress函数has_post_thumbnail()通过ID判断文章是否有缩略图/特色图片

描述:

通过ID判断文章是否有缩略图/特色图片

用法:

 <?php has_post_thumbnail( $post_id ); ?> 

参数:

$post_id

(integer) (可选) 文章ID

默认值: ‘ID’

示例:

<?php
// Must be inside a loop.

if ( has_post_thumbnail() ) {
	the_post_thumbnail();
}
else {
	echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg" />';
}
?>

源文件:

/**
 * Check if post has an image attached.
 *
 * @since 2.9.0
 *
 * @param int $post_id Optional. Post ID.
 * @return bool Whether post has an image attached.
 */
function has_post_thumbnail( $post_id = null ) {
	return (bool) get_post_thumbnail_id( $post_id );
}