37 */ |
37 */ |
38 return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id ); |
38 return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id ); |
39 } |
39 } |
40 |
40 |
41 /** |
41 /** |
42 * Retrieve post thumbnail ID. |
42 * Retrieves the post thumbnail ID. |
43 * |
43 * |
44 * @since 2.9.0 |
44 * @since 2.9.0 |
45 * @since 4.4.0 `$post` can be a post ID or WP_Post object. |
45 * @since 4.4.0 `$post` can be a post ID or WP_Post object. |
46 * @since 5.5.0 The return value for a non-existing post |
46 * @since 5.5.0 The return value for a non-existing post |
47 * was changed to false instead of an empty string. |
47 * was changed to false instead of an empty string. |
55 |
55 |
56 if ( ! $post ) { |
56 if ( ! $post ) { |
57 return false; |
57 return false; |
58 } |
58 } |
59 |
59 |
60 return (int) get_post_meta( $post->ID, '_thumbnail_id', true ); |
60 $thumbnail_id = (int) get_post_meta( $post->ID, '_thumbnail_id', true ); |
61 } |
61 |
62 |
62 /** |
63 /** |
63 * Filters the post thumbnail ID. |
64 * Display the post thumbnail. |
64 * |
|
65 * @since 5.9.0 |
|
66 * |
|
67 * @param int|false $thumbnail_id Post thumbnail ID or false if the post does not exist. |
|
68 * @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`. |
|
69 */ |
|
70 return (int) apply_filters( 'post_thumbnail_id', $thumbnail_id, $post ); |
|
71 } |
|
72 |
|
73 /** |
|
74 * Displays the post thumbnail. |
65 * |
75 * |
66 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
76 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
67 * is registered, which differs from the 'thumbnail' image size managed via the |
77 * is registered, which differs from the 'thumbnail' image size managed via the |
68 * Settings > Media screen. |
78 * Settings > Media screen. |
69 * |
79 * |
81 function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { |
91 function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { |
82 echo get_the_post_thumbnail( null, $size, $attr ); |
92 echo get_the_post_thumbnail( null, $size, $attr ); |
83 } |
93 } |
84 |
94 |
85 /** |
95 /** |
86 * Update cache for thumbnails in the current loop. |
96 * Updates cache for thumbnails in the current loop. |
87 * |
97 * |
88 * @since 3.2.0 |
98 * @since 3.2.0 |
89 * |
99 * |
90 * @global WP_Query $wp_query WordPress Query object. |
100 * @global WP_Query $wp_query WordPress Query object. |
91 * |
101 * |
115 |
125 |
116 $wp_query->thumbnails_cached = true; |
126 $wp_query->thumbnails_cached = true; |
117 } |
127 } |
118 |
128 |
119 /** |
129 /** |
120 * Retrieve the post thumbnail. |
130 * Retrieves the post thumbnail. |
121 * |
131 * |
122 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
132 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
123 * is registered, which differs from the 'thumbnail' image size managed via the |
133 * is registered, which differs from the 'thumbnail' image size managed via the |
124 * Settings > Media screen. |
134 * Settings > Media screen. |
125 * |
135 * |
174 |
184 |
175 if ( in_the_loop() ) { |
185 if ( in_the_loop() ) { |
176 update_post_thumbnail_cache(); |
186 update_post_thumbnail_cache(); |
177 } |
187 } |
178 |
188 |
|
189 // Get the 'loading' attribute value to use as default, taking precedence over the default from |
|
190 // `wp_get_attachment_image()`. |
|
191 $loading = wp_get_loading_attr_default( 'the_post_thumbnail' ); |
|
192 |
|
193 // Add the default to the given attributes unless they already include a 'loading' directive. |
|
194 if ( empty( $attr ) ) { |
|
195 $attr = array( 'loading' => $loading ); |
|
196 } elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) { |
|
197 $attr['loading'] = $loading; |
|
198 } elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) { |
|
199 $attr .= '&loading=' . $loading; |
|
200 } |
|
201 |
179 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); |
202 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); |
180 |
203 |
181 /** |
204 /** |
182 * Fires after fetching the post thumbnail HTML. |
205 * Fires after fetching the post thumbnail HTML. |
183 * |
206 * |
199 * |
222 * |
200 * @since 2.9.0 |
223 * @since 2.9.0 |
201 * |
224 * |
202 * @param string $html The post thumbnail HTML. |
225 * @param string $html The post thumbnail HTML. |
203 * @param int $post_id The post ID. |
226 * @param int $post_id The post ID. |
204 * @param int $post_thumbnail_id The post thumbnail ID. |
227 * @param int $post_thumbnail_id The post thumbnail ID, or 0 if there isn't one. |
205 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
228 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
206 * an array of width and height values in pixels (in that order). |
229 * an array of width and height values in pixels (in that order). |
207 * @param string $attr Query string of attributes. |
230 * @param string|array $attr Query string or array of attributes. |
208 */ |
231 */ |
209 return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr ); |
232 return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr ); |
210 } |
233 } |
211 |
234 |
212 /** |
235 /** |
213 * Return the post thumbnail URL. |
236 * Returns the post thumbnail URL. |
214 * |
237 * |
215 * @since 4.4.0 |
238 * @since 4.4.0 |
216 * |
239 * |
217 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
240 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
218 * @param string|int[] $size Optional. Registered image size to retrieve the source for or a flat array |
241 * @param string|int[] $size Optional. Registered image size to retrieve the source for or a flat array |
225 |
248 |
226 if ( ! $post_thumbnail_id ) { |
249 if ( ! $post_thumbnail_id ) { |
227 return false; |
250 return false; |
228 } |
251 } |
229 |
252 |
230 return wp_get_attachment_image_url( $post_thumbnail_id, $size ); |
253 $thumbnail_url = wp_get_attachment_image_url( $post_thumbnail_id, $size ); |
231 } |
254 |
232 |
255 /** |
233 /** |
256 * Filters the post thumbnail URL. |
234 * Display the post thumbnail URL. |
257 * |
|
258 * @since 5.9.0 |
|
259 * |
|
260 * @param string|false $thumbnail_url Post thumbnail URL or false if the post does not exist. |
|
261 * @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`. |
|
262 * @param string|int[] $size Registered image size to retrieve the source for or a flat array |
|
263 * of height and width dimensions. Default 'post-thumbnail'. |
|
264 */ |
|
265 return apply_filters( 'post_thumbnail_url', $thumbnail_url, $post, $size ); |
|
266 } |
|
267 |
|
268 /** |
|
269 * Displays the post thumbnail URL. |
235 * |
270 * |
236 * @since 4.4.0 |
271 * @since 4.4.0 |
237 * |
272 * |
238 * @param string|int[] $size Optional. Image size to use. Accepts any valid image size, |
273 * @param string|int[] $size Optional. Image size to use. Accepts any valid image size, |
239 * or an array of width and height values in pixels (in that order). |
274 * or an array of width and height values in pixels (in that order). |