Make WordPress Core


Ignore:
Timestamp:
07/05/2020 10:15:40 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Post Thumbnails: Change the return value of get_post_thumbnail_id() for a non-existing post to false instead of an empty string.

This further makes the function more consistent with get_the_ID() or wp_get_post_parent_id(), both returning false for a non-existing post.

Additionally, document that get_post_thumbnail_id() returns 0 if the thumbnail image is not set.

Follow-up to [47160].

Props theMikeD, dingo_d, netpassprodsr, SergeyBiryukov.
Fixes #49832. See #40096.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-thumbnail-template.php

    r47160 r48310  
    4444 * @since 2.9.0
    4545 * @since 4.4.0 `$post` can be a post ID or WP_Post object.
    46  *
    47  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
    48  * @return int|string Post thumbnail ID or empty string if the post does not exist.
     46 * @since 5.5.0 The return value for a non-existing post
     47 *              was changed to false instead of an empty string.
     48 *
     49 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
     50 * @return int|false Post thumbnail ID (which can be 0 if the thumbnail is not set),
     51 *                   or false if the post does not exist.
    4952 */
    5053function get_post_thumbnail_id( $post = null ) {
    5154    $post = get_post( $post );
     55
    5256    if ( ! $post ) {
    53         return '';
    54     }
     57        return false;
     58    }
     59
    5560    return (int) get_post_meta( $post->ID, '_thumbnail_id', true );
    5661}
     
    98103
    99104    $thumb_ids = array();
     105
    100106    foreach ( $wp_query->posts as $post ) {
    101107        $id = get_post_thumbnail_id( $post->ID );
     
    134140function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
    135141    $post = get_post( $post );
     142
    136143    if ( ! $post ) {
    137144        return '';
    138145    }
     146
    139147    $post_thumbnail_id = get_post_thumbnail_id( $post );
    140148
     
    166174         */
    167175        do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
     176
    168177        if ( in_the_loop() ) {
    169178            update_post_thumbnail_cache();
    170179        }
     180
    171181        $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
    172182
     
    186196        $html = '';
    187197    }
     198
    188199    /**
    189200     * Filters the post thumbnail HTML.
     
    213224function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
    214225    $post_thumbnail_id = get_post_thumbnail_id( $post );
     226
    215227    if ( ! $post_thumbnail_id ) {
    216228        return false;
    217229    }
     230
    218231    return wp_get_attachment_image_url( $post_thumbnail_id, $size );
    219232}
     
    230243function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
    231244    $url = get_the_post_thumbnail_url( null, $size );
     245
    232246    if ( $url ) {
    233247        echo esc_url( $url );
     
    245259function get_the_post_thumbnail_caption( $post = null ) {
    246260    $post_thumbnail_id = get_post_thumbnail_id( $post );
     261
    247262    if ( ! $post_thumbnail_id ) {
    248263        return '';
Note: See TracChangeset for help on using the changeset viewer.