wp/wp-includes/post-thumbnail-template.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    31 	 *
    31 	 *
    32 	 * @since 5.1.0
    32 	 * @since 5.1.0
    33 	 *
    33 	 *
    34 	 * @param bool             $has_thumbnail true if the post has a post thumbnail, otherwise false.
    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`.
    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.
    36 	 * @param int|false        $thumbnail_id  Post thumbnail ID or false if the post does not exist.
    37 	 */
    37 	 */
    38 	return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
    38 	return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
    39 }
    39 }
    40 
    40 
    41 /**
    41 /**
    72  *
    72  *
    73  * @since 2.9.0
    73  * @since 2.9.0
    74  *
    74  *
    75  * @see get_the_post_thumbnail()
    75  * @see get_the_post_thumbnail()
    76  *
    76  *
    77  * @param string|array $size Optional. Image size to use. Accepts any valid image size, or
    77  * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
    78  *                           an array of width and height values in pixels (in that order).
    78  *                           width and height values in pixels (in that order). Default 'post-thumbnail'.
    79  *                           Default 'post-thumbnail'.
       
    80  * @param string|array $attr Optional. Query string or array of attributes. Default empty.
    79  * @param string|array $attr Optional. Query string or array of attributes. Default empty.
    81  */
    80  */
    82 function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
    81 function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
    83 	echo get_the_post_thumbnail( null, $size, $attr );
    82 	echo get_the_post_thumbnail( null, $size, $attr );
    84 }
    83 }
   129  *
   128  *
   130  * @since 2.9.0
   129  * @since 2.9.0
   131  * @since 4.4.0 `$post` can be a post ID or WP_Post object.
   130  * @since 4.4.0 `$post` can be a post ID or WP_Post object.
   132  *
   131  *
   133  * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
   132  * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
   134  * @param string|array $size Optional. Image size to use. Accepts any valid image size, or
   133  * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
   135  *                           an array of width and height values in pixels (in that order).
   134  *                           width and height values in pixels (in that order). Default 'post-thumbnail'.
   136  *                           Default 'post-thumbnail'.
       
   137  * @param string|array $attr Optional. Query string or array of attributes. Default empty.
   135  * @param string|array $attr Optional. Query string or array of attributes. Default empty.
   138  * @return string The post thumbnail image tag.
   136  * @return string The post thumbnail image tag.
   139  */
   137  */
   140 function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
   138 function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
   141 	$post = get_post( $post );
   139 	$post = get_post( $post );
   150 	 * Filters the post thumbnail size.
   148 	 * Filters the post thumbnail size.
   151 	 *
   149 	 *
   152 	 * @since 2.9.0
   150 	 * @since 2.9.0
   153 	 * @since 4.9.0 Added the `$post_id` parameter.
   151 	 * @since 4.9.0 Added the `$post_id` parameter.
   154 	 *
   152 	 *
   155 	 * @param string|array $size    The post thumbnail size. Image size or array of width and height
   153 	 * @param string|int[] $size    Requested image size. Can be any registered image size name, or
   156 	 *                              values (in that order). Default 'post-thumbnail'.
   154 	 *                              an array of width and height values in pixels (in that order).
   157 	 * @param int          $post_id The post ID.
   155 	 * @param int          $post_id The post ID.
   158 	 */
   156 	 */
   159 	$size = apply_filters( 'post_thumbnail_size', $size, $post->ID );
   157 	$size = apply_filters( 'post_thumbnail_size', $size, $post->ID );
   160 
   158 
   161 	if ( $post_thumbnail_id ) {
   159 	if ( $post_thumbnail_id ) {
   166 		 * Provides "just in time" filtering of all filters in wp_get_attachment_image().
   164 		 * Provides "just in time" filtering of all filters in wp_get_attachment_image().
   167 		 *
   165 		 *
   168 		 * @since 2.9.0
   166 		 * @since 2.9.0
   169 		 *
   167 		 *
   170 		 * @param int          $post_id           The post ID.
   168 		 * @param int          $post_id           The post ID.
   171 		 * @param string       $post_thumbnail_id The post thumbnail ID.
   169 		 * @param int          $post_thumbnail_id The post thumbnail ID.
   172 		 * @param string|array $size              The post thumbnail size. Image size or array of width
   170 		 * @param string|int[] $size              Requested image size. Can be any registered image size name, or
   173 		 *                                        and height values (in that order). Default 'post-thumbnail'.
   171 		 *                                        an array of width and height values in pixels (in that order).
   174 		 */
   172 		 */
   175 		do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   173 		do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   176 
   174 
   177 		if ( in_the_loop() ) {
   175 		if ( in_the_loop() ) {
   178 			update_post_thumbnail_cache();
   176 			update_post_thumbnail_cache();
   184 		 * Fires after fetching the post thumbnail HTML.
   182 		 * Fires after fetching the post thumbnail HTML.
   185 		 *
   183 		 *
   186 		 * @since 2.9.0
   184 		 * @since 2.9.0
   187 		 *
   185 		 *
   188 		 * @param int          $post_id           The post ID.
   186 		 * @param int          $post_id           The post ID.
   189 		 * @param string       $post_thumbnail_id The post thumbnail ID.
   187 		 * @param int          $post_thumbnail_id The post thumbnail ID.
   190 		 * @param string|array $size              The post thumbnail size. Image size or array of width
   188 		 * @param string|int[] $size              Requested image size. Can be any registered image size name, or
   191 		 *                                        and height values (in that order). Default 'post-thumbnail'.
   189 		 *                                        an array of width and height values in pixels (in that order).
   192 		 */
   190 		 */
   193 		do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   191 		do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
   194 
   192 
   195 	} else {
   193 	} else {
   196 		$html = '';
   194 		$html = '';
   201 	 *
   199 	 *
   202 	 * @since 2.9.0
   200 	 * @since 2.9.0
   203 	 *
   201 	 *
   204 	 * @param string       $html              The post thumbnail HTML.
   202 	 * @param string       $html              The post thumbnail HTML.
   205 	 * @param int          $post_id           The post ID.
   203 	 * @param int          $post_id           The post ID.
   206 	 * @param string       $post_thumbnail_id The post thumbnail ID.
   204 	 * @param int          $post_thumbnail_id The post thumbnail ID.
   207 	 * @param string|array $size              The post thumbnail size. Image size or array of width and height
   205 	 * @param string|int[] $size              Requested image size. Can be any registered image size name, or
   208 	 *                                        values (in that order). Default 'post-thumbnail'.
   206 	 *                                        an array of width and height values in pixels (in that order).
   209 	 * @param string       $attr              Query string of attributes.
   207 	 * @param string       $attr              Query string of attributes.
   210 	 */
   208 	 */
   211 	return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
   209 	return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
   212 }
   210 }
   213 
   211 
   215  * Return the post thumbnail URL.
   213  * Return the post thumbnail URL.
   216  *
   214  *
   217  * @since 4.4.0
   215  * @since 4.4.0
   218  *
   216  *
   219  * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
   217  * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
   220  * @param string|array $size Optional. Registered image size to retrieve the source for or a flat
   218  * @param string|int[] $size Optional. Registered image size to retrieve the source for or a flat array
   221  *                           array of height and width dimensions. Default 'post-thumbnail'.
   219  *                           of height and width dimensions. Default 'post-thumbnail'.
   222  * @return string|false Post thumbnail URL or false if no URL is available.
   220  * @return string|false Post thumbnail URL or false if no image is available. If `$size` does not match
       
   221  *                      any registered image size, the original image URL will be returned.
   223  */
   222  */
   224 function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
   223 function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
   225 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   224 	$post_thumbnail_id = get_post_thumbnail_id( $post );
   226 
   225 
   227 	if ( ! $post_thumbnail_id ) {
   226 	if ( ! $post_thumbnail_id ) {
   234 /**
   233 /**
   235  * Display the post thumbnail URL.
   234  * Display the post thumbnail URL.
   236  *
   235  *
   237  * @since 4.4.0
   236  * @since 4.4.0
   238  *
   237  *
   239  * @param string|array $size Optional. Image size to use. Accepts any valid image size,
   238  * @param string|int[] $size Optional. Image size to use. Accepts any valid image size,
   240  *                           or an array of width and height values in pixels (in that order).
   239  *                           or an array of width and height values in pixels (in that order).
   241  *                           Default 'post-thumbnail'.
   240  *                           Default 'post-thumbnail'.
   242  */
   241  */
   243 function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
   242 function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
   244 	$url = get_the_post_thumbnail_url( null, $size );
   243 	$url = get_the_post_thumbnail_url( null, $size );