wp/wp-includes/post-thumbnail-template.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    17  * Conditional Tags} article in the Theme Developer Handbook.
    17  * Conditional Tags} article in the Theme Developer Handbook.
    18  *
    18  *
    19  * @since 2.9.0
    19  * @since 2.9.0
    20  * @since 4.4.0 `$post` can be a post ID or WP_Post object.
    20  * @since 4.4.0 `$post` can be a post ID or WP_Post object.
    21  *
    21  *
    22  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
    22  * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`.
    23  * @return bool Whether the post has an image attached.
    23  * @return bool Whether the post has an image attached.
    24  */
    24  */
    25 function has_post_thumbnail( $post = null ) {
    25 function has_post_thumbnail( $post = null ) {
    26 	$thumbnail_id  = get_post_thumbnail_id( $post );
    26 	$thumbnail_id  = get_post_thumbnail_id( $post );
    27 	$has_thumbnail = (bool) $thumbnail_id;
    27 	$has_thumbnail = (bool) $thumbnail_id;
    44  * @since 2.9.0
    44  * @since 2.9.0
    45  * @since 4.4.0 `$post` can be a post ID or WP_Post object.
    45  * @since 4.4.0 `$post` can be a post ID or WP_Post object.
    46  * @since 5.5.0 The return value for a non-existing post
    46  * @since 5.5.0 The return value for a non-existing post
    47  *              was changed to false instead of an empty string.
    47  *              was changed to false instead of an empty string.
    48  *
    48  *
    49  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
    49  * @param int|WP_Post|null $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),
    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.
    51  *                   or false if the post does not exist.
    52  */
    52  */
    53 function get_post_thumbnail_id( $post = null ) {
    53 function get_post_thumbnail_id( $post = null ) {
    54 	$post = get_post( $post );
    54 	$post = get_post( $post );
    97  *
    97  *
    98  * @since 3.2.0
    98  * @since 3.2.0
    99  *
    99  *
   100  * @global WP_Query $wp_query WordPress Query object.
   100  * @global WP_Query $wp_query WordPress Query object.
   101  *
   101  *
   102  * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
   102  * @param WP_Query|null $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
   103  */
   103  */
   104 function update_post_thumbnail_cache( $wp_query = null ) {
   104 function update_post_thumbnail_cache( $wp_query = null ) {
   105 	if ( ! $wp_query ) {
   105 	if ( ! $wp_query ) {
   106 		$wp_query = $GLOBALS['wp_query'];
   106 		$wp_query = $GLOBALS['wp_query'];
   107 	}
   107 	}
   110 		return;
   110 		return;
   111 	}
   111 	}
   112 
   112 
   113 	$thumb_ids = array();
   113 	$thumb_ids = array();
   114 
   114 
       
   115 	/*
       
   116 	 * $wp_query may contain an array of post objects or post IDs.
       
   117 	 *
       
   118 	 * This ensures the cache is primed for all post objects to avoid
       
   119 	 * `get_post()` calls in `get_the_post_thumbnail()` triggering an
       
   120 	 * additional database call for each post.
       
   121 	 */
       
   122 	$parent_post_ids = array();
   115 	foreach ( $wp_query->posts as $post ) {
   123 	foreach ( $wp_query->posts as $post ) {
   116 		$id = get_post_thumbnail_id( $post->ID );
   124 		if ( $post instanceof WP_Post ) {
       
   125 			$parent_post_ids[] = $post->ID;
       
   126 		} elseif ( is_int( $post ) ) {
       
   127 			$parent_post_ids[] = $post;
       
   128 		}
       
   129 	}
       
   130 	_prime_post_caches( $parent_post_ids, false, true );
       
   131 
       
   132 	foreach ( $wp_query->posts as $post ) {
       
   133 		$id = get_post_thumbnail_id( $post );
   117 		if ( $id ) {
   134 		if ( $id ) {
   118 			$thumb_ids[] = $id;
   135 			$thumb_ids[] = $id;
   119 		}
   136 		}
   120 	}
   137 	}
   121 
   138 
   137  * size is used by default, though a different size can be specified instead as needed.
   154  * size is used by default, though a different size can be specified instead as needed.
   138  *
   155  *
   139  * @since 2.9.0
   156  * @since 2.9.0
   140  * @since 4.4.0 `$post` can be a post ID or WP_Post object.
   157  * @since 4.4.0 `$post` can be a post ID or WP_Post object.
   141  *
   158  *
   142  * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
   159  * @param int|WP_Post|null $post Optional. Post ID or WP_Post object.  Default is global `$post`.
   143  * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
   160  * @param string|int[]     $size Optional. Image size. Accepts any registered image size name, or an array of
   144  *                           width and height values in pixels (in that order). Default 'post-thumbnail'.
   161  *                               width and height values in pixels (in that order). Default 'post-thumbnail'.
   145  * @param string|array $attr Optional. Query string or array of attributes. Default empty.
   162  * @param string|array     $attr Optional. Query string or array of attributes. Default empty.
   146  * @return string The post thumbnail image tag.
   163  * @return string The post thumbnail image tag.
   147  */
   164  */
   148 function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
   165 function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
   149 	$post = get_post( $post );
   166 	$post = get_post( $post );
   150 
   167 
   222 /**
   239 /**
   223  * Returns the post thumbnail URL.
   240  * Returns the post thumbnail URL.
   224  *
   241  *
   225  * @since 4.4.0
   242  * @since 4.4.0
   226  *
   243  *
   227  * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
   244  * @param int|WP_Post|null $post Optional. Post ID or WP_Post object.  Default is global `$post`.
   228  * @param string|int[] $size Optional. Registered image size to retrieve the source for or a flat array
   245  * @param string|int[]     $size Optional. Registered image size to retrieve the source for or a flat array
   229  *                           of height and width dimensions. Default 'post-thumbnail'.
   246  *                               of height and width dimensions. Default 'post-thumbnail'.
   230  * @return string|false Post thumbnail URL or false if no image is available. If `$size` does not match
   247  * @return string|false Post thumbnail URL or false if no image is available. If `$size` does not match
   231  *                      any registered image size, the original image URL will be returned.
   248  *                      any registered image size, the original image URL will be returned.
   232  */
   249  */
   233 function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
   250 function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
   234 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   251 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   272 /**
   289 /**
   273  * Returns the post thumbnail caption.
   290  * Returns the post thumbnail caption.
   274  *
   291  *
   275  * @since 4.6.0
   292  * @since 4.6.0
   276  *
   293  *
   277  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
   294  * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`.
   278  * @return string Post thumbnail caption.
   295  * @return string Post thumbnail caption.
   279  */
   296  */
   280 function get_the_post_thumbnail_caption( $post = null ) {
   297 function get_the_post_thumbnail_caption( $post = null ) {
   281 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   298 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   282 
   299 
   296 /**
   313 /**
   297  * Displays the post thumbnail caption.
   314  * Displays the post thumbnail caption.
   298  *
   315  *
   299  * @since 4.6.0
   316  * @since 4.6.0
   300  *
   317  *
   301  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
   318  * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global `$post`.
   302  */
   319  */
   303 function the_post_thumbnail_caption( $post = null ) {
   320 function the_post_thumbnail_caption( $post = null ) {
   304 	/**
   321 	/**
   305 	 * Filters the displayed post thumbnail caption.
   322 	 * Filters the displayed post thumbnail caption.
   306 	 *
   323 	 *