has_post_thumbnail

Sponsored Link

投稿に添付された画像が(true)かない(false)場合、booleanの値を返します。

注:ポストのサムネイルが有効にするには、現在のテーマのはfunctions.php ファイルにadd_theme_support( ‘post-thumbnails’ );を含めなければなりません。

Sponsored Link

Returns a boolean if post has an image attached (true) or not (false).

Note: To enable post thumbnails, the current theme must include add_theme_support( ‘post-thumbnails’ ); in its functions.php file.

has_post_thumbnailのテンプレートタグ使用方法

<?php has_post_thumbnail( $post_id ); ?> 

has_post_thumbnailのパラメーター

post_id

(integer) (optional)ポストIDです。

(integer) (optional) Post ID.

Default: ‘ID’, the post ID.

このスクリプトがある場合、実際には、記事にサムネイル表示させます。それがない場合は、デフォルトの画像を配置します。

This script asks if there is, actually, a thumbnail in the post. If it’s not, it will put a default image.

<?php 
//This must be in one loop

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

変更ログ

Since: 2.9.0

ソースファイル

has_post_thumbnail()は、下記に位置しています。

has_post_thumbnail() is located in

  • wp-includes/post-thumbnail-template.php (the latest stable version)
  • wp-includes/post-thumbnail-template.php (the development version)

関連テンプレートタグ

has_post_thumbnail, the_post_thumbnail, get_post_thumbnail_id, get_the_post_thumbnail

Sponsored Link