get_the_post_thumbnail() WordPress関数 テンプレートタグ

get_the_post_thumbnail()

構文get_the_post_thumbnail(①$post_id[オプション], ②$size[オプション], ③$attr[オプション]);

参照:WordPress Codex-get_the_post_thumbnail

get_the_post_thumbnail()の詳細

 動 作 

投稿やページ編集画面で設定できるアイキャッチ画像を取得する。

 返り値 

imgタグのHTMLを文字列で返す。

該当の画像が見つからない場合 → 空の文字列

 引 数 

①$post_id[オプション]

(整数) デフォルト:現在の投稿

②$size[オプション]

(文字列/配列) デフォルト:’post-thumbnail’

キーワードthumbnail,medium,large,full

HTMLのclass属性に反映される。

例)array(10,10)

③$attr[オプション]

attrはattributiveの短縮形 意味:属性の

(配列) デフォルト:なし

アイキャッチ画像を表示する際のimgタグ中に記述される属性と値を配列により設定。

<?php
$default_attr = array(
  'src'   => $src,	// アイキャッチ画像の URL
  'class' => "attachment-$size",	// 指定した大きさ
  'alt'   => trim( strip_tags( $attachment->post_excerpt ) ),	// アイキャッチ画像の抜粋
  'title' => trim( strip_tags( $attachment->post_title ) ),	// アイキャッチ画像のタイトル
);
?>

 

get_the_post_thumbnail()のコード

参照:wp-includes/post-thumbnail-template.php

<?php
114	/**
115	 * Retrieve the post thumbnail.
116	 *
117	 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
118	 * is registered, which differs from the 'thumbnail' image size managed via the
119	 * Settings > Media screen.
120	 *
121	 * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
122	 * size is used by default, though a different size can be specified instead as needed.
123	 *
124	 * @since 2.9.0
125	 * @since 4.4.0 `$post` can be a post ID or WP_Post object.
126	 *
127	 * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
128	 * @param string|array $size Optional. Image size to use. Accepts any valid image size, or
129	 *                           an array of width and height values in pixels (in that order).
130	 *                           Default 'post-thumbnail'.
131	 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
132	 * @return string The post thumbnail image tag.
133	 */
134	function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
135	        $post = get_post( $post );
136	        if ( ! $post ) {
137	                return '';
138	        }
139	        $post_thumbnail_id = get_post_thumbnail_id( $post );
140	
141	        /**
142	         * Filters the post thumbnail size.
143	         *
144	         * @since 2.9.0
145	         * @since 4.9.0 Added the `$post_id` parameter.
146	         *
147	         * @param string|array $size    The post thumbnail size. Image size or array of width and height
148	         *                              values (in that order). Default 'post-thumbnail'.
149	         * @param int          $post_id The post ID.
150	         */
151	        $size = apply_filters( 'post_thumbnail_size', $size, $post->ID );
152	
153	        if ( $post_thumbnail_id ) {
154	
155	                /**
156	                 * Fires before fetching the post thumbnail HTML.
157	                 *
158	                 * Provides "just in time" filtering of all filters in wp_get_attachment_image().
159	                 *
160	                 * @since 2.9.0
161	                 *
162	                 * @param int          $post_id           The post ID.
163	                 * @param string       $post_thumbnail_id The post thumbnail ID.
164	                 * @param string|array $size              The post thumbnail size. Image size or array of width
165	                 *                                        and height values (in that order). Default 'post-thumbnail'.
166	                 */
167	                do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
168	                if ( in_the_loop() ) {
169	                        update_post_thumbnail_cache();
170	                }
171	                $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
172	
173	                /**
174	                 * Fires after fetching the post thumbnail HTML.
175	                 *
176	                 * @since 2.9.0
177	                 *
178	                 * @param int          $post_id           The post ID.
179	                 * @param string       $post_thumbnail_id The post thumbnail ID.
180	                 * @param string|array $size              The post thumbnail size. Image size or array of width
181	                 *                                        and height values (in that order). Default 'post-thumbnail'.
182	                 */
183	                do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
184	
185	        } else {
186	                $html = '';
187	        }
188	        /**
189	         * Filters the post thumbnail HTML.
190	         *
191	         * @since 2.9.0
192	         *
193	         * @param string       $html              The post thumbnail HTML.
194	         * @param int          $post_id           The post ID.
195	         * @param string       $post_thumbnail_id The post thumbnail ID.
196	         * @param string|array $size              The post thumbnail size. Image size or array of width and height
197	         *                                        values (in that order). Default 'post-thumbnail'.
198	         * @param string       $attr              Query string of attributes.
199	         */
200	        return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
201	}
?>

get_post()

WordPressのおすすめ-カスタマイズ編

おすすめ

 

スポンサードリンク

スポンサードリンク