get_the_post_thumbnail()

You are here:

get_the_post_thumbnail( int|WP_Post $post = nullstring|array $size = ‘post-thumbnail’string|array $attr =  )

Retrieve the post thumbnail.

Description Description

When a theme adds ‘post-thumbnail’ support, a special ‘post-thumbnail’ image size is registered, which differs from the ‘thumbnail’ image size managed via the Settings > Media screen.

When using the_post_thumbnail() or related functions, the ‘post-thumbnail’ image size is used by default, though a different size can be specified instead as needed.


Top ↑

Parameters Parameters

$post

(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.

Default value: null

$size

(string|array) (Optional) Image size to use. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: ‘post-thumbnail’

$attr

(string|array) (Optional) Query string or array of attributes.

Default value: ”


Top ↑

Return Return

(string) The post thumbnail image tag.


Top ↑

More Information More Information

If the required add_theme_support( 'post-thumbnails' ); in the current theme’s functions.php file is attached to a hook, it must be must be called before the init hook is fired. The init hook may be too late for some features. If attached to a hook, it must be after_setup_theme.


Top ↑

Source Source

File: wp-includes/post-thumbnail-template.php

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
    $post = get_post( $post );
 
    if ( ! $post ) {
        return '';
    }
 
    $post_thumbnail_id = get_post_thumbnail_id( $post );
 
    /**
     * Filters the post thumbnail size.
     *
     * @since 2.9.0
     * @since 4.9.0 Added the `$post_id` parameter.
     *
     * @param string|array $size    The post thumbnail size. Image size or array of width and height
     *                              values (in that order). Default 'post-thumbnail'.
     * @param int          $post_id The post ID.
     */
    $size = apply_filters( 'post_thumbnail_size', $size, $post->ID );
 
    if ( $post_thumbnail_id ) {
 
        /**
         * Fires before fetching the post thumbnail HTML.
         *
         * Provides "just in time" filtering of all filters in wp_get_attachment_image().
         *
         * @since 2.9.0
         *
         * @param int          $post_id           The post ID.
         * @param string       $post_thumbnail_id The post thumbnail ID.
         * @param string|array $size              The post thumbnail size. Image size or array of width
         *                                        and height values (in that order). Default 'post-thumbnail'.
         */
        do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
 
        if ( in_the_loop() ) {
            update_post_thumbnail_cache();
        }
 
        $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
 
        /**
         * Fires after fetching the post thumbnail HTML.
         *
         * @since 2.9.0
         *
         * @param int          $post_id           The post ID.
         * @param string       $post_thumbnail_id The post thumbnail ID.
         * @param string|array $size              The post thumbnail size. Image size or array of width
         *                                        and height values (in that order). Default 'post-thumbnail'.
         */
        do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
 
    } else {
        $html = '';
    }
 
    /**
     * Filters the post thumbnail HTML.
     *
     * @since 2.9.0
     *
     * @param string       $html              The post thumbnail HTML.
     * @param int          $post_id           The post ID.
     * @param string       $post_thumbnail_id The post thumbnail ID.
     * @param string|array $size              The post thumbnail size. Image size or array of width and height
     *                                        values (in that order). Default 'post-thumbnail'.
     * @param string       $attr              Query string of attributes.
     */
    return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
}


Top ↑

Changelog Changelog

Changelog
VersionDescription
4.4.0$post can be a post ID or WP_Post object.
2.9.0Introduced.
Was this article helpful?
Dislike 0
Views: 2

Cart

Log in

Create an Account
Back to Top