3 * WordPress Post Thumbnail Template Functions.
5 * Support for post thumbnails.
6 * Theme's functions.php must call add_theme_support( 'post-thumbnails' ) to use these.
13 * Check if post has an image attached.
16 * @since 4.4.0 `$post` can be a post ID or WP_Post object.
18 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
19 * @return bool Whether the post has an image attached.
21 function has_post_thumbnail( $post = null ) {
22 return (bool) get_post_thumbnail_id( $post );
26 * Retrieve post thumbnail ID.
29 * @since 4.4.0 `$post` can be a post ID or WP_Post object.
31 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
32 * @return string|int Post thumbnail ID or empty string.
34 function get_post_thumbnail_id( $post = null ) {
35 $post = get_post( $post );
39 return get_post_meta( $post->ID, '_thumbnail_id', true );
43 * Display the post thumbnail.
45 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
46 * is registered, which differs from the 'thumbnail' image size managed via the
47 * Settings > Media screen.
49 * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
50 * size is used by default, though a different size can be specified instead as needed.
54 * @see get_the_post_thumbnail()
56 * @param string|array $size Optional. Image size to use. Accepts any valid image size, or
57 * an array of width and height values in pixels (in that order).
58 * Default 'post-thumbnail'.
59 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
61 function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
62 echo get_the_post_thumbnail( null, $size, $attr );
66 * Update cache for thumbnails in the current loop.
70 * @global WP_Query $wp_query
72 * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
74 function update_post_thumbnail_cache( $wp_query = null ) {
76 $wp_query = $GLOBALS['wp_query'];
78 if ( $wp_query->thumbnails_cached )
82 foreach ( $wp_query->posts as $post ) {
83 if ( $id = get_post_thumbnail_id( $post->ID ) )
87 if ( ! empty ( $thumb_ids ) ) {
88 _prime_post_caches( $thumb_ids, false, true );
91 $wp_query->thumbnails_cached = true;
95 * Retrieve the post thumbnail.
97 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
98 * is registered, which differs from the 'thumbnail' image size managed via the
99 * Settings > Media screen.
101 * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
102 * size is used by default, though a different size can be specified instead as needed.
105 * @since 4.4.0 `$post` can be a post ID or WP_Post object.
107 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
108 * @param string|array $size Optional. Image size to use. Accepts any valid image size, or
109 * an array of width and height values in pixels (in that order).
110 * Default 'post-thumbnail'.
111 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
112 * @return string The post thumbnail image tag.
114 function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
115 $post = get_post( $post );
119 $post_thumbnail_id = get_post_thumbnail_id( $post );
122 * Filter the post thumbnail size.
126 * @param string|array $size The post thumbnail size. Image size or array of width and height
127 * values (in that order). Default 'post-thumbnail'.
129 $size = apply_filters( 'post_thumbnail_size', $size );
131 if ( $post_thumbnail_id ) {
134 * Fires before fetching the post thumbnail HTML.
136 * Provides "just in time" filtering of all filters in wp_get_attachment_image().
140 * @param int $post_id The post ID.
141 * @param string $post_thumbnail_id The post thumbnail ID.
142 * @param string|array $size The post thumbnail size. Image size or array of width
143 * and height values (in that order). Default 'post-thumbnail'.
145 do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
147 update_post_thumbnail_cache();
148 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
151 * Fires after fetching the post thumbnail HTML.
155 * @param int $post_id The post ID.
156 * @param string $post_thumbnail_id The post thumbnail ID.
157 * @param string|array $size The post thumbnail size. Image size or array of width
158 * and height values (in that order). Default 'post-thumbnail'.
160 do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
166 * Filter the post thumbnail HTML.
170 * @param string $html The post thumbnail HTML.
171 * @param int $post_id The post ID.
172 * @param string $post_thumbnail_id The post thumbnail ID.
173 * @param string|array $size The post thumbnail size. Image size or array of width and height
174 * values (in that order). Default 'post-thumbnail'.
175 * @param string $attr Query string of attributes.
177 return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
181 * Return the post thumbnail URL.
185 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
186 * @param string|array $size Optional. Registered image size to retrieve the source for or a flat
187 * array of height and width dimensions. Default 'post-thumbnail'.
188 * @return string|false Post thumbnail URL or false if no URL is available.
190 function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
191 $post_thumbnail_id = get_post_thumbnail_id( $post );
192 if ( ! $post_thumbnail_id ) {
195 return wp_get_attachment_image_url( $post_thumbnail_id, $size );
199 * Display the post thumbnail URL.
203 * @param string|array $size Optional. Image size to use. Accepts any valid image size,
204 * or an array of width and height values in pixels (in that order).
205 * Default 'post-thumbnail'.
207 function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
208 $url = get_the_post_thumbnail_url( null, $size );
210 echo esc_url( $url );