author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
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 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
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`. |
|
36 |
* @param int|string $thumbnail_id Post thumbnail ID or empty string. |
|
37 |
*/ |
|
38 |
return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id ); |
|
0 | 39 |
} |
40 |
||
41 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
* Retrieve 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 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* @param int|WP_Post $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 |
|
60 |
return (int) get_post_meta( $post->ID, '_thumbnail_id', true ); |
|
0 | 61 |
} |
62 |
||
63 |
/** |
|
5 | 64 |
* Display the post thumbnail. |
65 |
* |
|
66 |
* 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 |
|
68 |
* Settings > Media screen. |
|
69 |
* |
|
70 |
* When using the_post_thumbnail() or related functions, the 'post-thumbnail' image |
|
71 |
* size is used by default, though a different size can be specified instead as needed. |
|
0 | 72 |
* |
73 |
* @since 2.9.0 |
|
74 |
* |
|
5 | 75 |
* @see get_the_post_thumbnail() |
76 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
* @param string|array $size Optional. Image size to use. Accepts any valid image size, or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* 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
|
79 |
* Default 'post-thumbnail'. |
5 | 80 |
* @param string|array $attr Optional. Query string or array of attributes. Default empty. |
0 | 81 |
*/ |
82 |
function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { |
|
83 |
echo get_the_post_thumbnail( null, $size, $attr ); |
|
84 |
} |
|
85 |
||
86 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* Update cache for thumbnails in the current loop. |
0 | 88 |
* |
5 | 89 |
* @since 3.2.0 |
0 | 90 |
* |
16 | 91 |
* @global WP_Query $wp_query WordPress Query object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
* @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global. |
0 | 94 |
*/ |
95 |
function update_post_thumbnail_cache( $wp_query = null ) { |
|
9 | 96 |
if ( ! $wp_query ) { |
0 | 97 |
$wp_query = $GLOBALS['wp_query']; |
9 | 98 |
} |
0 | 99 |
|
9 | 100 |
if ( $wp_query->thumbnails_cached ) { |
0 | 101 |
return; |
9 | 102 |
} |
0 | 103 |
|
104 |
$thumb_ids = array(); |
|
16 | 105 |
|
0 | 106 |
foreach ( $wp_query->posts as $post ) { |
16 | 107 |
$id = get_post_thumbnail_id( $post->ID ); |
108 |
if ( $id ) { |
|
0 | 109 |
$thumb_ids[] = $id; |
9 | 110 |
} |
0 | 111 |
} |
112 |
||
9 | 113 |
if ( ! empty( $thumb_ids ) ) { |
0 | 114 |
_prime_post_caches( $thumb_ids, false, true ); |
115 |
} |
|
116 |
||
117 |
$wp_query->thumbnails_cached = true; |
|
118 |
} |
|
119 |
||
120 |
/** |
|
5 | 121 |
* Retrieve the post thumbnail. |
122 |
* |
|
123 |
* When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
|
124 |
* is registered, which differs from the 'thumbnail' image size managed via the |
|
125 |
* Settings > Media screen. |
|
126 |
* |
|
127 |
* When using the_post_thumbnail() or related functions, the 'post-thumbnail' image |
|
128 |
* size is used by default, though a different size can be specified instead as needed. |
|
0 | 129 |
* |
130 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
* @since 4.4.0 `$post` can be a post ID or WP_Post object. |
0 | 132 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
* @param string|array $size Optional. Image size to use. Accepts any valid image size, or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* 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
|
136 |
* Default 'post-thumbnail'. |
5 | 137 |
* @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
|
138 |
* @return string The post thumbnail image tag. |
0 | 139 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
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
|
141 |
$post = get_post( $post ); |
16 | 142 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
} |
16 | 146 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
$post_thumbnail_id = get_post_thumbnail_id( $post ); |
0 | 148 |
|
149 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
* Filters the post thumbnail size. |
0 | 151 |
* |
152 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* @since 4.9.0 Added the `$post_id` parameter. |
0 | 154 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
* @param string|array $size The post thumbnail size. Image size or array of width and height |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
* values (in that order). Default 'post-thumbnail'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
* @param int $post_id The post ID. |
0 | 158 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
$size = apply_filters( 'post_thumbnail_size', $size, $post->ID ); |
0 | 160 |
|
161 |
if ( $post_thumbnail_id ) { |
|
162 |
||
163 |
/** |
|
164 |
* Fires before fetching the post thumbnail HTML. |
|
165 |
* |
|
166 |
* Provides "just in time" filtering of all filters in wp_get_attachment_image(). |
|
167 |
* |
|
168 |
* @since 2.9.0 |
|
169 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
* @param int $post_id The post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
* @param string $post_thumbnail_id The post thumbnail ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
* @param string|array $size The post thumbnail size. Image size or array of width |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
* and height values (in that order). Default 'post-thumbnail'. |
0 | 174 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size ); |
16 | 176 |
|
9 | 177 |
if ( in_the_loop() ) { |
0 | 178 |
update_post_thumbnail_cache(); |
9 | 179 |
} |
16 | 180 |
|
0 | 181 |
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); |
182 |
||
183 |
/** |
|
184 |
* Fires after fetching the post thumbnail HTML. |
|
185 |
* |
|
186 |
* @since 2.9.0 |
|
187 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
188 |
* @param int $post_id The post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
* @param string $post_thumbnail_id The post thumbnail ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
* @param string|array $size The post thumbnail size. Image size or array of width |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
* and height values (in that order). Default 'post-thumbnail'. |
0 | 192 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size ); |
0 | 194 |
|
195 |
} else { |
|
196 |
$html = ''; |
|
197 |
} |
|
16 | 198 |
|
0 | 199 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
* Filters the post thumbnail HTML. |
0 | 201 |
* |
202 |
* @since 2.9.0 |
|
203 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
* @param string $html The post thumbnail HTML. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
* @param int $post_id The post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
* @param string $post_thumbnail_id The post thumbnail ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
* @param string|array $size The post thumbnail size. Image size or array of width and height |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
* values (in that order). Default 'post-thumbnail'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
* @param string $attr Query string of attributes. |
0 | 210 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
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
|
212 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
* Return the post thumbnail URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
216 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
* @param string|array $size Optional. Registered image size to retrieve the source for or a flat |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
* array of height and width dimensions. Default 'post-thumbnail'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
* @return string|false Post thumbnail URL or false if no URL is available. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
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
|
225 |
$post_thumbnail_id = get_post_thumbnail_id( $post ); |
16 | 226 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
if ( ! $post_thumbnail_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
} |
16 | 230 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
return wp_get_attachment_image_url( $post_thumbnail_id, $size ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
* Display the post thumbnail URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
* @since 4.4.0 |
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 |
* @param string|array $size Optional. Image size to use. Accepts any valid image size, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
* 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
|
241 |
* Default 'post-thumbnail'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
function the_post_thumbnail_url( $size = 'post-thumbnail' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
$url = get_the_post_thumbnail_url( null, $size ); |
16 | 245 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
if ( $url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
echo esc_url( $url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
} |
0 | 249 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
* Returns the post thumbnail caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
* @return string Post thumbnail caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
function get_the_post_thumbnail_caption( $post = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
$post_thumbnail_id = get_post_thumbnail_id( $post ); |
16 | 261 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
if ( ! $post_thumbnail_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
$caption = wp_get_attachment_caption( $post_thumbnail_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
if ( ! $caption ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
$caption = ''; |
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 |
return $caption; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
273 |
} |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
* Displays the post thumbnail caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
function the_post_thumbnail_caption( $post = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
* Filters the displayed post thumbnail caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
* @param string $caption Caption for the given attachment. |
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 |
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
|
291 |
} |