author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Post Thumbnail Template Functions. |
|
4 |
* |
|
5 | 5 |
* Support for post thumbnails. |
6 |
* Theme's functions.php must call add_theme_support( 'post-thumbnails' ) to use these. |
|
0 | 7 |
* |
8 |
* @package WordPress |
|
9 |
* @subpackage Template |
|
10 |
*/ |
|
11 |
||
12 |
/** |
|
9 | 13 |
* Determines whether a post has an image attached. |
14 |
* |
|
15 |
* For more information on this and similar theme functions, check out |
|
16 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
17 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 18 |
* |
19 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* @since 4.4.0 `$post` can be a post ID or WP_Post object. |
0 | 21 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
22 |
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* @return bool Whether the post has an image attached. |
0 | 24 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
function has_post_thumbnail( $post = null ) { |
9 | 26 |
$thumbnail_id = get_post_thumbnail_id( $post ); |
27 |
$has_thumbnail = (bool) $thumbnail_id; |
|
28 |
||
29 |
/** |
|
30 |
* Filters whether a post has a post thumbnail. |
|
31 |
* |
|
32 |
* @since 5.1.0 |
|
33 |
* |
|
34 |
* @param bool $has_thumbnail true if the post has a post thumbnail, otherwise false. |
|
35 |
* @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`. |
|
18 | 36 |
* @param int|false $thumbnail_id Post thumbnail ID or false if the post does not exist. |
9 | 37 |
*/ |
38 |
return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id ); |
|
0 | 39 |
} |
40 |
||
41 |
/** |
|
19 | 42 |
* Retrieves the post thumbnail ID. |
0 | 43 |
* |
44 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* @since 4.4.0 `$post` can be a post ID or WP_Post object. |
16 | 46 |
* @since 5.5.0 The return value for a non-existing post |
47 |
* was changed to false instead of an empty string. |
|
0 | 48 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
49 |
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`. |
16 | 50 |
* @return int|false Post thumbnail ID (which can be 0 if the thumbnail is not set), |
51 |
* or false if the post does not exist. |
|
0 | 52 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
function get_post_thumbnail_id( $post = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
$post = get_post( $post ); |
16 | 55 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
if ( ! $post ) { |
16 | 57 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
} |
16 | 59 |
|
19 | 60 |
$thumbnail_id = (int) get_post_meta( $post->ID, '_thumbnail_id', true ); |
61 |
||
62 |
/** |
|
63 |
* Filters the post thumbnail ID. |
|
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 ); |
|
0 | 71 |
} |
72 |
||
73 |
/** |
|
19 | 74 |
* Displays the post thumbnail. |
5 | 75 |
* |
76 |
* When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
|
77 |
* is registered, which differs from the 'thumbnail' image size managed via the |
|
78 |
* Settings > Media screen. |
|
79 |
* |
|
80 |
* When using the_post_thumbnail() or related functions, the 'post-thumbnail' image |
|
81 |
* size is used by default, though a different size can be specified instead as needed. |
|
0 | 82 |
* |
83 |
* @since 2.9.0 |
|
84 |
* |
|
5 | 85 |
* @see get_the_post_thumbnail() |
86 |
* |
|
18 | 87 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
88 |
* width and height values in pixels (in that order). Default 'post-thumbnail'. |
|
5 | 89 |
* @param string|array $attr Optional. Query string or array of attributes. Default empty. |
0 | 90 |
*/ |
91 |
function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { |
|
92 |
echo get_the_post_thumbnail( null, $size, $attr ); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
19 | 96 |
* Updates cache for thumbnails in the current loop. |
0 | 97 |
* |
5 | 98 |
* @since 3.2.0 |
0 | 99 |
* |
16 | 100 |
* @global WP_Query $wp_query WordPress Query object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
102 |
* @param WP_Query|null $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global. |
0 | 103 |
*/ |
104 |
function update_post_thumbnail_cache( $wp_query = null ) { |
|
9 | 105 |
if ( ! $wp_query ) { |
0 | 106 |
$wp_query = $GLOBALS['wp_query']; |
9 | 107 |
} |
0 | 108 |
|
9 | 109 |
if ( $wp_query->thumbnails_cached ) { |
0 | 110 |
return; |
9 | 111 |
} |
0 | 112 |
|
113 |
$thumb_ids = array(); |
|
16 | 114 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
115 |
/* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
116 |
* $wp_query may contain an array of post objects or post IDs. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
117 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
118 |
* This ensures the cache is primed for all post objects to avoid |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
119 |
* `get_post()` calls in `get_the_post_thumbnail()` triggering an |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
120 |
* additional database call for each post. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
121 |
*/ |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
122 |
$parent_post_ids = array(); |
0 | 123 |
foreach ( $wp_query->posts as $post ) { |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
124 |
if ( $post instanceof WP_Post ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
125 |
$parent_post_ids[] = $post->ID; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
126 |
} elseif ( is_int( $post ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
127 |
$parent_post_ids[] = $post; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
128 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
129 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
130 |
_prime_post_caches( $parent_post_ids, false, true ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
131 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
132 |
foreach ( $wp_query->posts as $post ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
133 |
$id = get_post_thumbnail_id( $post ); |
16 | 134 |
if ( $id ) { |
0 | 135 |
$thumb_ids[] = $id; |
9 | 136 |
} |
0 | 137 |
} |
138 |
||
9 | 139 |
if ( ! empty( $thumb_ids ) ) { |
0 | 140 |
_prime_post_caches( $thumb_ids, false, true ); |
141 |
} |
|
142 |
||
143 |
$wp_query->thumbnails_cached = true; |
|
144 |
} |
|
145 |
||
146 |
/** |
|
19 | 147 |
* Retrieves the post thumbnail. |
5 | 148 |
* |
149 |
* When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
|
150 |
* is registered, which differs from the 'thumbnail' image size managed via the |
|
151 |
* Settings > Media screen. |
|
152 |
* |
|
153 |
* When using the_post_thumbnail() or related functions, the 'post-thumbnail' image |
|
154 |
* size is used by default, though a different size can be specified instead as needed. |
|
0 | 155 |
* |
156 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
* @since 4.4.0 `$post` can be a post ID or WP_Post object. |
0 | 158 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
159 |
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
160 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
161 |
* width and height values in pixels (in that order). Default 'post-thumbnail'. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
162 |
* @param string|array $attr Optional. Query string or array of attributes. Default empty. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
* @return string The post thumbnail image tag. |
0 | 164 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
$post = get_post( $post ); |
16 | 167 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
} |
16 | 171 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
$post_thumbnail_id = get_post_thumbnail_id( $post ); |
0 | 173 |
|
174 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
* Filters the post thumbnail size. |
0 | 176 |
* |
177 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* @since 4.9.0 Added the `$post_id` parameter. |
0 | 179 |
* |
18 | 180 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
181 |
* an array of width and height values in pixels (in that order). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* @param int $post_id The post ID. |
0 | 183 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
$size = apply_filters( 'post_thumbnail_size', $size, $post->ID ); |
0 | 185 |
|
186 |
if ( $post_thumbnail_id ) { |
|
187 |
||
188 |
/** |
|
189 |
* Fires before fetching the post thumbnail HTML. |
|
190 |
* |
|
191 |
* Provides "just in time" filtering of all filters in wp_get_attachment_image(). |
|
192 |
* |
|
193 |
* @since 2.9.0 |
|
194 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* @param int $post_id The post ID. |
18 | 196 |
* @param int $post_thumbnail_id The post thumbnail ID. |
197 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
198 |
* an array of width and height values in pixels (in that order). |
|
0 | 199 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size ); |
16 | 201 |
|
9 | 202 |
if ( in_the_loop() ) { |
0 | 203 |
update_post_thumbnail_cache(); |
9 | 204 |
} |
16 | 205 |
|
0 | 206 |
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); |
207 |
||
208 |
/** |
|
209 |
* Fires after fetching the post thumbnail HTML. |
|
210 |
* |
|
211 |
* @since 2.9.0 |
|
212 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
* @param int $post_id The post ID. |
18 | 214 |
* @param int $post_thumbnail_id The post thumbnail ID. |
215 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
216 |
* an array of width and height values in pixels (in that order). |
|
0 | 217 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size ); |
0 | 219 |
|
220 |
} else { |
|
221 |
$html = ''; |
|
222 |
} |
|
16 | 223 |
|
0 | 224 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* Filters the post thumbnail HTML. |
0 | 226 |
* |
227 |
* @since 2.9.0 |
|
228 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
* @param string $html The post thumbnail HTML. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
* @param int $post_id The post ID. |
19 | 231 |
* @param int $post_thumbnail_id The post thumbnail ID, or 0 if there isn't one. |
18 | 232 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
233 |
* an array of width and height values in pixels (in that order). |
|
19 | 234 |
* @param string|array $attr Query string or array of attributes. |
0 | 235 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
/** |
19 | 240 |
* Returns the post thumbnail URL. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
244 |
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
245 |
* @param string|int[] $size Optional. Registered image size to retrieve the source for or a flat array |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
246 |
* of height and width dimensions. Default 'post-thumbnail'. |
18 | 247 |
* @return string|false Post thumbnail URL or false if no image is available. If `$size` does not match |
248 |
* any registered image size, the original image URL will be returned. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
$post_thumbnail_id = get_post_thumbnail_id( $post ); |
16 | 252 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
if ( ! $post_thumbnail_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
} |
16 | 256 |
|
19 | 257 |
$thumbnail_url = wp_get_attachment_image_url( $post_thumbnail_id, $size ); |
258 |
||
259 |
/** |
|
260 |
* Filters the post thumbnail URL. |
|
261 |
* |
|
262 |
* @since 5.9.0 |
|
263 |
* |
|
264 |
* @param string|false $thumbnail_url Post thumbnail URL or false if the post does not exist. |
|
265 |
* @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`. |
|
266 |
* @param string|int[] $size Registered image size to retrieve the source for or a flat array |
|
267 |
* of height and width dimensions. Default 'post-thumbnail'. |
|
268 |
*/ |
|
269 |
return apply_filters( 'post_thumbnail_url', $thumbnail_url, $post, $size ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
/** |
19 | 273 |
* Displays the post thumbnail URL. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
* |
18 | 277 |
* @param string|int[] $size Optional. Image size to use. Accepts any valid image size, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
* or an array of width and height values in pixels (in that order). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
* Default 'post-thumbnail'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
function the_post_thumbnail_url( $size = 'post-thumbnail' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
$url = get_the_post_thumbnail_url( null, $size ); |
16 | 283 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
if ( $url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
echo esc_url( $url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
} |
0 | 287 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
289 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
* Returns the post thumbnail caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
292 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
294 |
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
295 |
* @return string Post thumbnail caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
function get_the_post_thumbnail_caption( $post = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
$post_thumbnail_id = get_post_thumbnail_id( $post ); |
16 | 299 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
if ( ! $post_thumbnail_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
$caption = wp_get_attachment_caption( $post_thumbnail_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
if ( ! $caption ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
$caption = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
return $caption; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
* Displays the post thumbnail caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
318 |
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
function the_post_thumbnail_caption( $post = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
* Filters the displayed post thumbnail caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* @param string $caption Caption for the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
} |