get_the_post_thumbnail()

最后更新于:2021-11-27 02:32:27

get_the_post_thumbnail( int|GC_Post$post=null, string|int[]$size=’post-thumbnail’, string|array$attr=”)

Retrieve the post thumbnail.

参数

$post

(int|GC_Post) (Optional) Post ID or GC_Post object. Default is global $post.

Default value: null

$size

(string|int[]) (Optional) Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).

Default value: ‘post-thumbnail’

$attr

(string|array) (Optional) Query string or array of attributes.

Default value: ”

响应

(string) The post thumbnail image tag.

源文件

文件: gc-includes/post-thumbnail-template.php

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 gc_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();
		}

		$html = gc_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.
	 * @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       $attr              Query string of attributes.
	 */
	return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
}
/**
 * Link thumbnails to their posts based on attr
 *
 * @param $html
 * @param int $pid
 * @param int $post_thumbnail_id
 * @param int $size
 * @param array $attr
 *
 * @return string
 */

function gcdev_filter_post_thumbnail_html( $html, $pid, $post_thumbnail_id, $size, $attr ) {

	 if ( ! empty( $attr[ 'link_thumbnail' ] ) ) {

		$html = sprintf(
			'<a href="https://docs.gechiui.com/functions/get_the_post_thumbnail/%s" title="%s" rel="nofollow">%s</a>',
			get_permalink( $pid ),
			esc_attr( get_the_title( $pid ) ),
			$html
		);
	 }

	return $html;
}

add_filter( 'post_thumbnail_html', 'gcdev_filter_post_thumbnail_html', 10, 5 );