wp/wp-includes/post-thumbnail-template.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     8  * @package WordPress
     8  * @package WordPress
     9  * @subpackage Template
     9  * @subpackage Template
    10  */
    10  */
    11 
    11 
    12 /**
    12 /**
    13  * Check if post has an image attached.
    13  * Determines whether a post has an image attached.
       
    14  *
       
    15  * For more information on this and similar theme functions, check out
       
    16  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
       
    17  * Conditional Tags} article in the Theme Developer Handbook.
    14  *
    18  *
    15  * @since 2.9.0
    19  * @since 2.9.0
    16  * @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.
    17  *
    21  *
    18  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
    22  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
    19  * @return bool Whether the post has an image attached.
    23  * @return bool Whether the post has an image attached.
    20  */
    24  */
    21 function has_post_thumbnail( $post = null ) {
    25 function has_post_thumbnail( $post = null ) {
    22 	return (bool) get_post_thumbnail_id( $post );
    26 	$thumbnail_id  = get_post_thumbnail_id( $post );
       
    27 	$has_thumbnail = (bool) $thumbnail_id;
       
    28 
       
    29 	/**
       
    30 	 * Filters whether a post has a post thumbnail.
       
    31 	 *
       
    32 	 * @since 5.1.0
       
    33 	 *
       
    34 	 * @param bool             $has_thumbnail true if the post has a post thumbnail, otherwise false.
       
    35 	 * @param int|WP_Post|null $post          Post ID or WP_Post object. Default is global `$post`.
       
    36 	 * @param int|string       $thumbnail_id  Post thumbnail ID or empty string.
       
    37 	 */
       
    38 	return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
    23 }
    39 }
    24 
    40 
    25 /**
    41 /**
    26  * Retrieve post thumbnail ID.
    42  * Retrieve post thumbnail ID.
    27  *
    43  *
    70  * @global WP_Query $wp_query
    86  * @global WP_Query $wp_query
    71  *
    87  *
    72  * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
    88  * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
    73  */
    89  */
    74 function update_post_thumbnail_cache( $wp_query = null ) {
    90 function update_post_thumbnail_cache( $wp_query = null ) {
    75 	if ( ! $wp_query )
    91 	if ( ! $wp_query ) {
    76 		$wp_query = $GLOBALS['wp_query'];
    92 		$wp_query = $GLOBALS['wp_query'];
    77 
    93 	}
    78 	if ( $wp_query->thumbnails_cached )
    94 
       
    95 	if ( $wp_query->thumbnails_cached ) {
    79 		return;
    96 		return;
       
    97 	}
    80 
    98 
    81 	$thumb_ids = array();
    99 	$thumb_ids = array();
    82 	foreach ( $wp_query->posts as $post ) {
   100 	foreach ( $wp_query->posts as $post ) {
    83 		if ( $id = get_post_thumbnail_id( $post->ID ) )
   101 		if ( $id = get_post_thumbnail_id( $post->ID ) ) {
    84 			$thumb_ids[] = $id;
   102 			$thumb_ids[] = $id;
    85 	}
   103 		}
    86 
   104 	}
    87 	if ( ! empty ( $thumb_ids ) ) {
   105 
       
   106 	if ( ! empty( $thumb_ids ) ) {
    88 		_prime_post_caches( $thumb_ids, false, true );
   107 		_prime_post_caches( $thumb_ids, false, true );
    89 	}
   108 	}
    90 
   109 
    91 	$wp_query->thumbnails_cached = true;
   110 	$wp_query->thumbnails_cached = true;
    92 }
   111 }
   143 		 * @param string       $post_thumbnail_id The post thumbnail ID.
   162 		 * @param string       $post_thumbnail_id The post thumbnail ID.
   144 		 * @param string|array $size              The post thumbnail size. Image size or array of width
   163 		 * @param string|array $size              The post thumbnail size. Image size or array of width
   145 		 *                                        and height values (in that order). Default 'post-thumbnail'.
   164 		 *                                        and height values (in that order). Default 'post-thumbnail'.
   146 		 */
   165 		 */
   147 		do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   166 		do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   148 		if ( in_the_loop() )
   167 		if ( in_the_loop() ) {
   149 			update_post_thumbnail_cache();
   168 			update_post_thumbnail_cache();
       
   169 		}
   150 		$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
   170 		$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
   151 
   171 
   152 		/**
   172 		/**
   153 		 * Fires after fetching the post thumbnail HTML.
   173 		 * Fires after fetching the post thumbnail HTML.
   154 		 *
   174 		 *