WordPress.org

Make WordPress Core

Ticket #15447: 15447.5.diff

File 15447.5.diff, 1.7 KB (added by garyc40, 7 years ago)

prevent notice for $wp_query->_thumbnails_cached

  • wp-includes/post-thumbnail-template.php

    diff --git wp-includes/post-thumbnail-template.php wp-includes/post-thumbnail-template.php
    index 3758d1d..f1c897a 100644
    function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { 
    4848}
    4949
    5050/**
     51 * Update cache for thumbnails in current loop
     52 *
     53 * @since 3.2
     54 */
     55function update_thumbnail_cache() {
     56        global $wp_query;
     57       
     58        // return if this loop is already cached
     59        if ( empty( $wp_query->_thumbnails_cached ) )
     60                return;
     61
     62        $thumb_ids = array();
     63        foreach ( $wp_query->posts as $post ) {
     64                if ( $id  = get_post_thumbnail_id( $post->ID ) )
     65                        $thumb_ids[] = $id;
     66        }
     67
     68        if ( ! empty( $thumb_ids ) ) {
     69                $thumb_ids = array_filter( array_unique( $thumb_ids ) );
     70
     71                get_posts( array(
     72                        'update_post_term_cache' => false,
     73                        'include'                => $thumb_ids,
     74                        'post_type'              => 'attachment',
     75                        'post_status'            => 'inherit',
     76                        'nopaging'               => true,
     77                ) );
     78        }
     79       
     80        $wp_query->_thumbnails_cached = true;
     81}
     82
     83/**
    5184 * Retrieve Post Thumbnail.
    5285 *
    5386 * @since 2.9.0
    function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $att 
    6295        $size = apply_filters( 'post_thumbnail_size', $size );
    6396        if ( $post_thumbnail_id ) {
    6497                do_action( 'begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size ); // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters
     98
     99                if ( in_the_loop() )
     100                        update_thumbnail_cache();
     101
    65102                $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
    66103                do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
    67104        } else {