WordPress函数get_the_post_thumbnail()检索帖子缩略图

WordPress函数get_the_post_thumbnail()检索帖子缩略图

WordPress函数get_the_post_thumbnail()检索帖子缩略图

  在 WordPress CMS内容管理系统中,get_the_post_thumbnail()是一个内置函数,获取帖子的特色图像。该函数有两个参数:帖子 ID 或帖子对象、要获取的图像的大小。图像的大小可以是注册的图像大小名称,也可以是以像素为单位的宽度和高度值的数组(按该顺序)。默认大小为post-thumbnail,这是 WordPress 管理区域中使用的大小。

  当主题添加“缩略图后”支持时,会注册特殊的“缩略图后”图像尺寸,该尺寸与通过“设置”>“媒体”屏幕管理的“缩略图”图像尺寸不同。当使用the_post_thumbnail() 或相关函数时,默认使用“后缩略图”图像大小,但可以根据需要指定不同的大小。

  推荐:The Plus Addon For Elementor插件Elementor扩展插件

get_the_post_thumbnail()函数基本语法

get_the_post_thumbnail($post, $size, $attr)

描述

  检索帖子缩略图

参数

  $post

  (int|WP_Post)(可选):帖子 ID 或 WP_Post 对象。默认是全局的$post默认:null

  $size

  (string|int)(可选)图像大小。接受任何已注册的图像大小名称或以像素为单位的宽度和高度值数组(按该顺序)。默认“’post-thumbnail’”

  $attr

  (string|array) (可选)查询字符串或属性数组。默认: ”

  推荐:8个最佳Chrome ChatGPT扩展程序

get_the_post_thumbnail()函数

  get_the_post_thumbnail() 是一个方便的函数,可以轻松显示帖子的特色图像,而无需知道图像 URL 或手动生成 HTML。它抽象了从数据库检索图像数据的细节(源文件可参考这里

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

		// Add `loading` attribute.
		if ( wp_lazy_loading_enabled( 'img', 'the_post_thumbnail' ) ) {
			// 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 );
}

  推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons

如何使用get_the_post_thumbnail()

  以默认大小显示帖子缩略图,此示例使用默认的“帖子缩略图”大小显示帖子缩略图。

echo get_the_post_thumbnail($post_id);

  显示自定义尺寸的帖子缩略图,此示例使用 200×200 像素的自定义尺寸显示帖子缩略图。

echo get_the_post_thumbnail($post_id, array(200, 200));

  显示带有附加类别的帖子缩略图,此示例显示添加了“alignleft”类的帖子缩略图。

echo get_the_post_thumbnail($post_id, 'medium', array('class' => 'alignleft'));

  显示带有 alt 属性的帖子缩略图,此示例显示带有自定义 alt 属性的帖子缩略图。

echo get_the_post_thumbnail($post_id, 'medium', array('alt' => 'Custom alt text'));

  在循环中显示帖子缩略图,此示例在循环中显示每个帖子的帖子缩略图。

if (have_posts()) :
    while (have_posts()) : the_post();
        echo get_the_post_thumbnail(get_the_ID(), 'medium');
    endwhile;
endif;

  推荐:[最新版]WP Optimize Premium插件下载WordPress优化插件

  推荐:WordPress函数使用手册

5/5 - (1 vote)

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部