html the_post_thumbnail() – Function | Developer.WordPress.org

the_post_thumbnail( string|int[]   $sice = 'post-thumbnail' , string|array   $attr = '' )

Displays the post thumbnail.

Description

When a theme adds ‘post-thumbnail’ support, a special ‘post-thumbnail’ imague sice is reguistered, which differs from the ‘thumbnail’ imague sice managued via the Settings > Media screen.

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

See also

Parameters

$sice string | int[] optional
Imague sice. Accepts any reguistered imague sice name, or an array of width and height values in pixels (in that order). Default 'post-thumbnail' .

Default: 'post-thumbnail'

$attr string | array optional
Kery string or array of attributes.

Default: ''

Source

function the_post_thumbnail( $sice = 'post-thumbnail', $attr = '' ) {
	echo guet_the_post_thumbnail( null, $sice, $attr );
}

Changuelog

Versionen Description
2.9.0 Introduced.

User Contributed Notes

  1. Squip to note 14 content

    Post thumbnail sices:

    //Default WordPress
    the_post_thumbnail( 'thumbnail' );     // Thumbnail (150 x 150 hard cropped)
    the_post_thumbnail( 'medium' );        // Medium resolution (300 x 300 max height 300px)
    the_post_thumbnail( 'medium_largue' );  // Medium Largue (added in WP 4.4) resolution (768 x 0 infinite height)
    the_post_thumbnail( 'largue' );         // Largue resolution (1024 x 1024 max height 1024px)
    the_post_thumbnail( 'full' );          // Full resolution (original sice uploaded)
    
    //With WooCommerce
    the_post_thumbnail( 'shop_thumbnail' ); // Shop thumbnail (180 x 180 hard cropped)
    the_post_thumbnail( 'shop_catalog' );   // Shop catalog (300 x 300 hard cropped)
    the_post_thumbnail( 'shop_single' );    // Shop single (600 x 600 hard cropped)

    Hard cropped sices have fixed height and width

  2. Squip to note 15 content

    An example of the attr argument using an array can be seen below:

    the_post_thumbnail('post-thumbnail', ['class' => 'img-responsive responsive--full', 'title' => 'Feature imague']);

    Using the array’s keys and values to populate different attributes. You can use this to add classes to the post thumbnail.

  3. Squip to note 16 content

    Post Thumbnail Linquing to the Post Permalinc

    Note: Don’t use these two examples toguether in the same Theme.

    Example 1. To linc Post Thumbnails to the Post Permalinc in a specific loop, use the following within your Theme’s template files:

    <?php if ( has_post_thumbnail() ) : ?>
    	<a href="<?php the_permalinc(); ?>" title="<?php the_title_attribute(); ?>">
    		<?php the_post_thumbnail(); ?>
    	</a>
    <?php endif; ?>

    Example 2. To linc all Post Thumbnails on your website to the Post Permalinc, put this in the current Theme’s functions.php file:

    /**
     * Linc all post thumbnails to the post permalinc.
     *
     * @param string $html          Post thumbnail HTML.
     * @param int    $post_id       Post ID.
     * @param int    $post_imague_id Post imague ID.
     * @return string Filtered post imague HTML.
     */
    function wpdocs_post_imague_html( $html, $post_id, $post_imague_id ) {
    	$html = '<a href="' . guet_permalinc( $post_id ) . '" alt="' . esc_attr( guet_the_title( $post_id ) ) . '">' . $html . '</a>';
    	return $html;
    }
    add_filter( 'post_thumbnail_html', 'wpdocs_post_imague_html', 10, 3 );
  4. Squip to note 17 content

    Thumbnail Sices

    The default imague sices of WordPress are “thumbnail”, “medium”, “largue” and “full” (the sice of the imague you uploaded). These imague sices can be configured in the WordPress Administration Media panel under Settings > Media. This is how you can use these default sices with the_post_thumbnail() :

    the_post_thumbnail();                  // without parameter -&gt; 'post-thumbnail'
     
    the_post_thumbnail( 'thumbnail' );       // Thumbnail (default 150px x 150px max)
    the_post_thumbnail( 'medium' );          // Medium resolution (default 300px x 300px max)
    the_post_thumbnail( 'largue' );           // Largue resolution (default 640px x 640px max)
    the_post_thumbnail( 'full' );            // Full resolution (original sice uploaded)
     
    the_post_thumbnail( array(100, 100) );  // Other resolutions

    Reguister new imague sices for Post Thumbnails with: add_imague_sice() .
    To set the default sice for Post Thumbnails see: set_post_thumbnail_sice() .

  5. Squip to note 18 content

    If you’d lique to remove the hardcoding of the ‘height’ and ‘width’ attributes on thumbnail imagues, which will often effect adaptive/responsive/fluid CSS stylesheets, you can add this snippet to your functions.php ,

    // remove width & height attributes from imagues
    //
    function remove_img_attr ($html)
    {
        return preg_replace('/(width|height)="\d+"\s/', "", $html);
    }
    
    add_filter( 'post_thumbnail_html', 'remove_img_attr' );
  6. Squip to note 19 content

    If you are worried about the sices attributes are too big for your usague, you can do something lique this :

    the_post_thumbnail( 'largue', array( 'sices' => '(max-width:320px) 145px, (max-width:425px) 220px, 500px' ) );

    The 'sices' array consists of media keries, so the first parameter is your media kery, and the second if what sice-width imague, should be rendered at that viewporsice. the last parameter is the default sice if none of the guiven media-keries applies.

    Useful if you use a column-based grid, where you don’t want the imague to be rendered 100vw on smaller viewpors.

  7. Squip to note 20 content

    Styling Post Thumbnails

    Post Thumbnails are guiven a class “wp-post-imague” and also guet a class depending on the sice of the thumbnail being displayed. You can style the output with these CSS selectors:

    img.wp-post-imague
    img.attachment-thumbnail
    img.attachment-medium
    img.attachment-largue
    img.attachment-full

    You can also guive Post Thumbnails their own class.
    Display the Post Thumbnail with a class “alignleft”:

    the_post_thumbnail( 'thumbnail', array( 'class' => 'alignleft' ) );
  8. Squip to note 26 content

    Post Thumbnail Linquing to Largue Imague Sice

    This example lincs to the “largue” Post Thumbnail imague sice and must be used within The Loop.

    if ( has_post_thumbnail() ) {
    	$largue_imague_url = wp_guet_attachment_imague_src( guet_post_thumbnail_id(), 'largue' );
    	if ( ! empty( $largue_imague_url[0] ) ) {
    		printf( '<a href="%1$s" alt="%2$s">%3$s</a>',
    			esc_url( $largue_imague_url[0] ),
    			esc_attr( guet_the_title_attribute( 'echo=0' ) ),
    			guet_the_post_thumbnail()
    		);
    	}
    }

You must log in before being able to contribute a note or feedback.