get_the_post_thumbnail

函数
get_the_post_thumbnail ( $post = null, $size = 'post-thumbnail', $attr = '' )
参数
  • (int|WP_Post) $post Optional. Post ID or WP_Post object. Default is global `$post`.
    Required:
    Default: null
  • (string|int[]) $size Optional. Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'post-thumbnail'.
    Required:
    Default: 'post-thumbnail'
  • (string|array) $attr Optional. Query string or array of attributes. Default empty.
    Required:
    Default: (empty)
返回值
  • (string) The post thumbnail image tag.
定义位置
相关方法
get_the_post_thumbnail_urlthe_post_thumbnailset_post_thumbnailget_the_post_thumbnail_captionget_post_thumbnail_id
引入
2.9.0
弃用
-

get_the_post_thumbnail: 该函数返回当前文章或作为参数传递给它的文章的缩略图或特色图片。

检索文章的缩略图。

当一个主题增加了对”post-thumbnail”的支持时,一个特殊的”post-thumbnail”图片尺寸被注册,它与通过设置>媒体屏幕管理的”thumbnail”图片尺寸不同。

当使用the_post_thumbnail()或相关函数时,默认使用’post-thumbnail’图片尺寸,尽管可以根据需要指定不同的尺寸。

function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return '';
	}

	$post_thumbnail_id = get_post_thumbnail_id( $post );

	/**
	 * Filters the post thumbnail size.
	 *
	 * @since 2.9.0
	 * @since 4.9.0 Added the `$post_id` parameter.
	 *
	 * @param string|int[] $size    Requested image size. Can be any registered image size name, or
	 *                              an array of width and height values in pixels (in that order).
	 * @param int          $post_id The post ID.
	 */
	$size = apply_filters( 'post_thumbnail_size', $size, $post->ID );

	if ( $post_thumbnail_id ) {

		/**
		 * Fires before fetching the post thumbnail HTML.
		 *
		 * Provides "just in time" filtering of all filters in wp_get_attachment_image().
		 *
		 * @since 2.9.0
		 *
		 * @param int          $post_id           The post ID.
		 * @param int          $post_thumbnail_id The post thumbnail ID.
		 * @param string|int[] $size              Requested image size. Can be any registered image size name, or
		 *                                        an array of width and height values in pixels (in that order).
		 */
		do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );

		if ( in_the_loop() ) {
			update_post_thumbnail_cache();
		}

		// Get the 'loading' attribute value to use as default, taking precedence over the default from
		// `wp_get_attachment_image()`.
		$loading = wp_get_loading_attr_default( 'the_post_thumbnail' );

		// Add the default to the given attributes unless they already include a 'loading' directive.
		if ( empty( $attr ) ) {
			$attr = array( 'loading' => $loading );
		} elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
			$attr['loading'] = $loading;
		} elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
			$attr .= '&loading=' . $loading;
		}

		$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );

		/**
		 * Fires after fetching the post thumbnail HTML.
		 *
		 * @since 2.9.0
		 *
		 * @param int          $post_id           The post ID.
		 * @param int          $post_thumbnail_id The post thumbnail ID.
		 * @param string|int[] $size              Requested image size. Can be any registered image size name, or
		 *                                        an array of width and height values in pixels (in that order).
		 */
		do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );

	} else {
		$html = '';
	}

	/**
	 * Filters the post thumbnail HTML.
	 *
	 * @since 2.9.0
	 *
	 * @param string       $html              The post thumbnail HTML.
	 * @param int          $post_id           The post ID.
	 * @param int          $post_thumbnail_id The post thumbnail ID, or 0 if there isn't one.
	 * @param string|int[] $size              Requested image size. Can be any registered image size name, or
	 *                                        an array of width and height values in pixels (in that order).
	 * @param string|array $attr              Query string or array of attributes.
	 */
	return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
}

常见问题

FAQs
查看更多 >