wp/wp-includes/post-thumbnail-template.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    41 /**
    41 /**
    42  * Retrieve post thumbnail ID.
    42  * Retrieve post thumbnail ID.
    43  *
    43  *
    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  *
    46  * @since 5.5.0 The return value for a non-existing post
    47  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
    47  *              was changed to false instead of an empty string.
    48  * @return string|int Post thumbnail ID or 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.
    49  */
    52  */
    50 function get_post_thumbnail_id( $post = null ) {
    53 function get_post_thumbnail_id( $post = null ) {
    51 	$post = get_post( $post );
    54 	$post = get_post( $post );
       
    55 
    52 	if ( ! $post ) {
    56 	if ( ! $post ) {
    53 		return '';
    57 		return false;
    54 	}
    58 	}
    55 	return get_post_meta( $post->ID, '_thumbnail_id', true );
    59 
       
    60 	return (int) get_post_meta( $post->ID, '_thumbnail_id', true );
    56 }
    61 }
    57 
    62 
    58 /**
    63 /**
    59  * Display the post thumbnail.
    64  * Display the post thumbnail.
    60  *
    65  *
    81 /**
    86 /**
    82  * Update cache for thumbnails in the current loop.
    87  * Update cache for thumbnails in the current loop.
    83  *
    88  *
    84  * @since 3.2.0
    89  * @since 3.2.0
    85  *
    90  *
    86  * @global WP_Query $wp_query
    91  * @global WP_Query $wp_query WordPress Query object.
    87  *
    92  *
    88  * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
    93  * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
    89  */
    94  */
    90 function update_post_thumbnail_cache( $wp_query = null ) {
    95 function update_post_thumbnail_cache( $wp_query = null ) {
    91 	if ( ! $wp_query ) {
    96 	if ( ! $wp_query ) {
    95 	if ( $wp_query->thumbnails_cached ) {
   100 	if ( $wp_query->thumbnails_cached ) {
    96 		return;
   101 		return;
    97 	}
   102 	}
    98 
   103 
    99 	$thumb_ids = array();
   104 	$thumb_ids = array();
       
   105 
   100 	foreach ( $wp_query->posts as $post ) {
   106 	foreach ( $wp_query->posts as $post ) {
   101 		if ( $id = get_post_thumbnail_id( $post->ID ) ) {
   107 		$id = get_post_thumbnail_id( $post->ID );
       
   108 		if ( $id ) {
   102 			$thumb_ids[] = $id;
   109 			$thumb_ids[] = $id;
   103 		}
   110 		}
   104 	}
   111 	}
   105 
   112 
   106 	if ( ! empty( $thumb_ids ) ) {
   113 	if ( ! empty( $thumb_ids ) ) {
   130  * @param string|array $attr Optional. Query string or array of attributes. Default empty.
   137  * @param string|array $attr Optional. Query string or array of attributes. Default empty.
   131  * @return string The post thumbnail image tag.
   138  * @return string The post thumbnail image tag.
   132  */
   139  */
   133 function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
   140 function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
   134 	$post = get_post( $post );
   141 	$post = get_post( $post );
       
   142 
   135 	if ( ! $post ) {
   143 	if ( ! $post ) {
   136 		return '';
   144 		return '';
   137 	}
   145 	}
       
   146 
   138 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   147 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   139 
   148 
   140 	/**
   149 	/**
   141 	 * Filters the post thumbnail size.
   150 	 * Filters the post thumbnail size.
   142 	 *
   151 	 *
   162 		 * @param string       $post_thumbnail_id The post thumbnail ID.
   171 		 * @param string       $post_thumbnail_id The post thumbnail ID.
   163 		 * @param string|array $size              The post thumbnail size. Image size or array of width
   172 		 * @param string|array $size              The post thumbnail size. Image size or array of width
   164 		 *                                        and height values (in that order). Default 'post-thumbnail'.
   173 		 *                                        and height values (in that order). Default 'post-thumbnail'.
   165 		 */
   174 		 */
   166 		do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   175 		do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
       
   176 
   167 		if ( in_the_loop() ) {
   177 		if ( in_the_loop() ) {
   168 			update_post_thumbnail_cache();
   178 			update_post_thumbnail_cache();
   169 		}
   179 		}
       
   180 
   170 		$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
   181 		$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
   171 
   182 
   172 		/**
   183 		/**
   173 		 * Fires after fetching the post thumbnail HTML.
   184 		 * Fires after fetching the post thumbnail HTML.
   174 		 *
   185 		 *
   182 		do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   193 		do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   183 
   194 
   184 	} else {
   195 	} else {
   185 		$html = '';
   196 		$html = '';
   186 	}
   197 	}
       
   198 
   187 	/**
   199 	/**
   188 	 * Filters the post thumbnail HTML.
   200 	 * Filters the post thumbnail HTML.
   189 	 *
   201 	 *
   190 	 * @since 2.9.0
   202 	 * @since 2.9.0
   191 	 *
   203 	 *
   209  *                           array of height and width dimensions. Default 'post-thumbnail'.
   221  *                           array of height and width dimensions. Default 'post-thumbnail'.
   210  * @return string|false Post thumbnail URL or false if no URL is available.
   222  * @return string|false Post thumbnail URL or false if no URL is available.
   211  */
   223  */
   212 function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
   224 function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
   213 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   225 	$post_thumbnail_id = get_post_thumbnail_id( $post );
       
   226 
   214 	if ( ! $post_thumbnail_id ) {
   227 	if ( ! $post_thumbnail_id ) {
   215 		return false;
   228 		return false;
   216 	}
   229 	}
       
   230 
   217 	return wp_get_attachment_image_url( $post_thumbnail_id, $size );
   231 	return wp_get_attachment_image_url( $post_thumbnail_id, $size );
   218 }
   232 }
   219 
   233 
   220 /**
   234 /**
   221  * Display the post thumbnail URL.
   235  * Display the post thumbnail URL.
   226  *                           or an array of width and height values in pixels (in that order).
   240  *                           or an array of width and height values in pixels (in that order).
   227  *                           Default 'post-thumbnail'.
   241  *                           Default 'post-thumbnail'.
   228  */
   242  */
   229 function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
   243 function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
   230 	$url = get_the_post_thumbnail_url( null, $size );
   244 	$url = get_the_post_thumbnail_url( null, $size );
       
   245 
   231 	if ( $url ) {
   246 	if ( $url ) {
   232 		echo esc_url( $url );
   247 		echo esc_url( $url );
   233 	}
   248 	}
   234 }
   249 }
   235 
   250 
   241  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
   256  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
   242  * @return string Post thumbnail caption.
   257  * @return string Post thumbnail caption.
   243  */
   258  */
   244 function get_the_post_thumbnail_caption( $post = null ) {
   259 function get_the_post_thumbnail_caption( $post = null ) {
   245 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   260 	$post_thumbnail_id = get_post_thumbnail_id( $post );
       
   261 
   246 	if ( ! $post_thumbnail_id ) {
   262 	if ( ! $post_thumbnail_id ) {
   247 		return '';
   263 		return '';
   248 	}
   264 	}
   249 
   265 
   250 	$caption = wp_get_attachment_caption( $post_thumbnail_id );
   266 	$caption = wp_get_attachment_caption( $post_thumbnail_id );