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.
17 * @param int $post_id Optional. Post ID.
18 * @return bool Whether post has an image attached.
20 function has_post_thumbnail( $post_id = null ) {
21 return (bool) get_post_thumbnail_id( $post_id );
25 * Retrieve Post Thumbnail ID.
29 * @param int|null $post_id Optional. Post ID.
32 function get_post_thumbnail_id( $post_id = null ) {
33 $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
34 return get_post_meta( $post_id, '_thumbnail_id', true );
38 * Display the post thumbnail.
40 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
41 * is registered, which differs from the 'thumbnail' image size managed via the
42 * Settings > Media screen.
44 * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
45 * size is used by default, though a different size can be specified instead as needed.
49 * @see get_the_post_thumbnail()
51 * @param string|array $size Optional. Registered image size to use, or flat array of height
52 * and width values. Default 'post-thumbnail'.
53 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
55 function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
56 echo get_the_post_thumbnail( null, $size, $attr );
60 * Update cache for thumbnails in the current loop
64 * @global WP_Query $wp_query
66 * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
68 function update_post_thumbnail_cache( $wp_query = null ) {
70 $wp_query = $GLOBALS['wp_query'];
72 if ( $wp_query->thumbnails_cached )
76 foreach ( $wp_query->posts as $post ) {
77 if ( $id = get_post_thumbnail_id( $post->ID ) )
81 if ( ! empty ( $thumb_ids ) ) {
82 _prime_post_caches( $thumb_ids, false, true );
85 $wp_query->thumbnails_cached = true;
89 * Retrieve the post thumbnail.
91 * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
92 * is registered, which differs from the 'thumbnail' image size managed via the
93 * Settings > Media screen.
95 * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
96 * size is used by default, though a different size can be specified instead as needed.
100 * @param int $post_id Post ID. Default is the ID of the `$post` global.
101 * @param string|array $size Optional. Registered image size to use, or flat array of height
102 * and width values. Default 'post-thumbnail'.
103 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
106 function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' ) {
107 $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
108 $post_thumbnail_id = get_post_thumbnail_id( $post_id );
111 * Filter the post thumbnail size.
115 * @param string $size The post thumbnail size.
117 $size = apply_filters( 'post_thumbnail_size', $size );
119 if ( $post_thumbnail_id ) {
122 * Fires before fetching the post thumbnail HTML.
124 * Provides "just in time" filtering of all filters in wp_get_attachment_image().
128 * @param string $post_id The post ID.
129 * @param string $post_thumbnail_id The post thumbnail ID.
130 * @param string $size The post thumbnail size.
132 do_action( 'begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
134 update_post_thumbnail_cache();
135 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
138 * Fires after fetching the post thumbnail HTML.
142 * @param string $post_id The post ID.
143 * @param string $post_thumbnail_id The post thumbnail ID.
144 * @param string $size The post thumbnail size.
146 do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
152 * Filter the post thumbnail HTML.
156 * @param string $html The post thumbnail HTML.
157 * @param string $post_id The post ID.
158 * @param string $post_thumbnail_id The post thumbnail ID.
159 * @param string $size The post thumbnail size.
160 * @param string $attr Query string of attributes.
162 return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr );