This page redirects to an external site: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/
Languages: English • Italiano • 日本語 (Add your language)
Gets the Featured Image (formerly called Post Thumbnail) as set in post's or page's edit screen and returns an HTML image element representing a Featured Image, if there is any, otherwise an empty string.
Note: To enable Post Thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
$default_attr = array( 'src' => $src, 'class' => "attachment-$size", 'alt' => trim( strip_tags( $attachment->post_excerpt ) ), 'title' => trim( strip_tags( $attachment->post_title ) ), );
Returns a string containing an HTML image element, or an empty string if no post thumbnail is found.
<?php $pages = get_pages( array( 'child_of' => 1 ) ); ?> <ul> <?php foreach ( $pages as $page ) : ?> <li> <?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?> <h1><?php echo apply_filters( 'the_title', $page->post_title, $page->ID ); ?></h1> <?php echo apply_filters( 'the_content', $page->post_content ); ?> </li> <?php endforeach; ?> </ul>
The default image sizes of WordPress are "thumbnail", "medium", "large" and "full" (the size of the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media. Themes may also add "post-thumbnail". This is how you can use these default sizes with get_the_post_thumbnail():
// without parameter -> Post Thumbnail (as set by theme using set_post_thumbnail_size()) get_the_post_thumbnail( $post_id ); get_the_post_thumbnail( $post_id, 'thumbnail' ); // Thumbnail (Note: different to Post Thumbnail) get_the_post_thumbnail( $post_id, 'medium' ); // Medium resolution get_the_post_thumbnail( $post_id, 'large' ); // Large resolution get_the_post_thumbnail( $post_id, 'full' ); // Original resolution get_the_post_thumbnail( $post_id, array( 100, 100) ); // Other resolutions
Register new image sizes for Post Thumbnails with: add_image_size().
To set the default size for Post Thumbnails see: set_post_thumbnail_size().
This example shows the 5 latest Post Thumbnails linked to their Post permalink.
<?php
$thumbnails = get_posts( 'numberposts=5' );
foreach ( $thumbnails as $thumbnail ) {
if ( has_post_thumbnail( $thumbnail->ID ) ) {
echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
echo get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
echo '</a>';
}
}
?>
This example links to the "large" Post Thumbnail image size and must be used within The Loop.
<?php
if ( has_post_thumbnail() ) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '" >';
echo get_the_post_thumbnail( $post->ID, 'thumbnail' );
echo '</a>';
}
?>
Post Thumbnails are given a class "wp-post-image". They also get a class depending on the size of the thumbnail being displayed You can style the output with these CSS selectors:
img.wp-post-image img.attachment-thumbnail img.attachment-medium img.attachment-large img.attachment-full
You can also give Post Thumbnails their own class.
Give the Post Thumbnail a class "alignleft".
<?php echo get_the_post_thumbnail( $post_id, 'thumbnail', array( 'class' => 'alignleft' ) ); ?>
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.
Since: 2.9.0
get_the_post_thumbnail() is located in wp-includes/post-thumbnail-template.php.