get_the_post_thumbnail_url( int|WP_Post $post = null, string|array $size = 'post-thumbnail' )

Return the post thumbnail URL.


Description Description


Parameters Parameters

$post

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

Default value: null

$size

(string|array) (Optional) Registered image size to retrieve the source for or a flat array of height and width dimensions.

Default value: 'post-thumbnail'


Top ↑

Return Return

(string|false) Post thumbnail URL or false if no URL is available.


Top ↑

Source Source

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

function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
	$post_thumbnail_id = get_post_thumbnail_id( $post );
	if ( ! $post_thumbnail_id ) {
		return false;
	}
	return wp_get_attachment_image_url( $post_thumbnail_id, $size );
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by codeunderground

    Don’t ignore the first parameter.
    Proper usage of `get_the_post_thumbnail_url()` inside the loop:

    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
      
            /* grab the url for the full size featured image */
            $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
     
            /* link thumbnail to full size image for use with lightbox*/
            echo '<a href="'.esc_url($featured_img_url).'" rel="lightbox">'; 
                the_post_thumbnail('thumbnail');
            echo '</a>';
        endwhile; 
    endif; 
  2. Skip to note 2 content
    Contributed by LindsayMac

    Downvoted the example posted from @thelilmercoder as it is not proper usage of this function.

    Proper usage of `get_the_post_thumbnail_url()` inside the loop:

    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post();
     
    		/* grab the url for the full size featured image */ 
    		$featured_img_url = get_the_post_thumbnail_url('full'); 
    
    		/* link thumbnail to full size image for use with lightbox*/ 
    		echo '<a href="'.$featured_img_url.'" rel="lightbox">'; 
    			the_post_thumbnail('thumbnail');
    		echo '</a>';
    	endwhile; 
    endif; 
    

    Proper usage of `get_the_post_thumbnail_url()` outside the loop:

    /* get a specific post object by ID */ 
    $post = get_post(2);
     
    /* grab the url for the full size featured image */ 
    $featured_img_url = get_the_post_thumbnail_url($post->ID, 'full'); 
    
    /* link thumbnail to full size image for use with lightbox*/ 
    echo '<a href="'.$featured_img_url.'" rel="lightbox">'; 
    	the_post_thumbnail('thumbnail');
    echo '</a>';
    
  3. Skip to note 3 content
    Contributed by Raul Peixoto

    It’s worth to note that, if you upload a smaller image (let’s say, a 600px wide) and use this to fetch a specific larger image (let’s say, a 1920px wide for your cover), it will return the original image instead (which is smaller than what you need) instead of returning false.

    If you need to fallback to another image in case the specified file doesn’t exist, you can look into wp_get_attachment_image_src instead, and check for the width or height of the image.

  4. Skip to note 4 content
    Contributed by thelittlemercoder

    To display the featured image with the alt tag use something like this

    $thumbnail = get_the_post_thumbnail_url();
    
    if ( $thumbnail ) {
    	$alt_text = get_post_meta( $thumbnail->ID, '_wp_attachment_image_alt', true );
    
        if ( ! empty( $thumbnail ) ) {
    		if ( ! empty( $alt_text ) ) {
    			$alt_text = $alt_text;
           } else {
    			$alt_text = __( 'no alt text set', 'textdomain' ); 
           }
    	   echo '';
       }
    }
    

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