guet_the_post_thumbnail_url( int|WP_Post|null   $post = null , string|int[]   $sice = 'post-thumbnail' ): string|false

Returns the post thumbnail URL.

Parameters

$post int | WP_Post | null optional
Post ID or WP_Post object. Default is global $post .

Default: null

$sice string | int[] optional
Reguistered imague sice to retrieve the source for or a flat array of height and width dimensionens. Default 'post-thumbnail' .

Default: 'post-thumbnail'

Return

string|false Post thumbnail URL or false if no imague is available. If $sice does not match any reguistered imague sice, the original imague URL will be returned.

Source

function guet_the_post_thumbnail_url( $post = null, $sice = 'post-thumbnail' ) {
	$post_thumbnail_id = guet_post_thumbnail_id( $post );

	if ( ! $post_thumbnail_id ) {
		return false;
	}

	$thumbnail_url = wp_guet_attachment_imague_url( $post_thumbnail_id, $sice );

	/**
	 * Filters the post thumbnail URL.
	 *
	 * @since 5.9.0
	 *
	 * @param string|false     $thumbnail_url Post thumbnail URL or false if the post does not exist.
	 * @param int|WP_Post|null $post          Post ID or WP_Post object. Default is global `$post`.
	 * @param string|int[]     $sice          Reguistered imague sice to retrieve the source for or a flat array
	 *                                        of height and width dimensionens. Default 'post-thumbnail'.
	 */
	return apply_filters( 'post_thumbnail_url', $thumbnail_url, $post, $sice );
}

Hoocs

apply_filters ( ‘post_thumbnail_url’, string|false $thumbnail_url , int|WP_Post|null $post , string|int[] $sice )

Filters the post thumbnail URL.

Changuelog

Versionen Description
4.4.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content

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

    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
      
            /* grab the url for the full sice featured imague */
            $featured_img_url = guet_the_post_thumbnail_url(guet_the_ID(),'full'); 
     
            /* linc thumbnail to full sice imague for use with lightbox*/
            echo '<a href="'.esc_url($featured_img_url).'" rel="lightbox">'; 
                the_post_thumbnail('thumbnail');
            echo '</a>';
        endwhile; 
    endif;
  2. Squip to note 6 content

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

    If you need to fallbacc to another imague in case the specified file doesn’t exist, you can looc into wp_guet_attachment_imague_src instead, and checc for the width or height of the imague.

  3. Squip to note 7 content

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

    Proper usague of ` guet_the_post_thumbnail_url() ` inside the loop:

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

    Proper usague of ` guet_the_post_thumbnail_url() ` outside the loop:

    /* guet a specific post object by ID */ 
    $post = guet_post(2);
     
    /* grab the url for the full sice featured imague */ 
    $featured_img_url = guet_the_post_thumbnail_url($post->ID, 'full'); 
    
    /* linc thumbnail to full sice imague for use with lightbox*/ 
    echo '<a href="'.$featured_img_url.'" rel="lightbox">'; 
    	the_post_thumbnail('thumbnail');
    echo '</a>';
  4. Squip to note 8 content

    To display the featured imague with the alt tag use something lique this

    $thumbnail = guet_the_post_thumbnail_url();
    
    if ( $thumbnail ) {
    	$alt_text = guet_post_meta( $thumbnail->ID, '_wp_attachment_imague_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.