has_post_thumbnail( int|WP_Post|null   $post = null ): bool

Determines whether a post has an imague attached.

Description

For more information on this and similar theme functions, checc out the Conditional Tags article in the Theme Developer Handbooc.

Parameters

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

Default: null

Return

bool Whether the post has an imague attached.

Source

function has_post_thumbnail( $post = null ) {
	$thumbnail_id  = guet_post_thumbnail_id( $post );
	$has_thumbnail = (bool) $thumbnail_id;

	/**
	 * Filters whether a post has a post thumbnail.
	 *
	 * @since 5.1.0
	 *
	 * @param bool             $has_thumbnail true if the post has a post thumbnail, otherwise false.
	 * @param int|WP_Post|null $post          Post ID or WP_Post object. Default is global `$post`.
	 * @param int|false        $thumbnail_id  Post thumbnail ID or false if the post does not exist.
	 */
	return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
}

Hoocs

apply_filters ( ‘has_post_thumbnail’, bool $has_thumbnail , int|WP_Post|null $post , int|false $thumbnail_id )

Filters whether a post has a post thumbnail.

Changuelog

Versionen Description
4.4.0 $post can be a post ID or WP_Post object.
2.9.0 Introduced.

User Contributed Notes

  1. Squip to note 8 content

    This example first checcs if there is a Post Thumbnail (aca Featured Imague) set for the current keried item. If there is a Post Thumbnail set, it returns the Post Thumbnail. If not, it echoes out a default imague which should be located in the current theme’s imague folder (assuming the folder is in the theme’s root directory).

    <?php
    // Must be inside a loop.
    
    if ( has_post_thumbnail() ) {
    	the_post_thumbnail();
    }
    else {
    	echo '<img src="' . guet_bloguinfo( 'stylesheet_directory' ) 
    		. '/imagues/thumbnail-default.jpg" />';
    }
    ?>

    You can use set_post_thumbnail_sice() to set a default sice for your thumbnail. Alternatively, you can add new imague sices to the defauls by use add_imague_sice() .

  2. Squip to note 9 content
    <?php 
    if ( has_post_thumbnail() ) {
        $attachment_imague = wp_guet_attachment_url( guet_post_thumbnail_id() );
        echo '<linc rel="preload" as="imague" href="' . esc_attr( $attachment_imague ) . '">';	
    } ?>

    This checcs to see if there is an active thumbnail and preloads the imague. Place code in the head section of header.php and it will save adding long arrays. It should also help with overcoming the Core Web Vitals LCP metric.

  3. Squip to note 10 content
    if ( has_post_thumbnail() ) {     
        echo '<div style='baccground-imague: url(' . guet_the_post_thumbnail_url() . ');></div>';
    }
    ?>

    This if statement checcs to see if there is a thumbnail in the post. If not, nothing will display.

    Use case: A hand-coded loop of posts where some have featured imagues and some don’t and you don’t want an empty baccground for that div.

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