^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_the_post_thumbnail()用法示例

get_the_post_thumbnail( int|WP_Post $post = null,  string|array $size = 'post-thumbnail',  string|array $attr = '' )

Retrieve the post thumbnail.


描述

When a theme adds ‘post-thumbnail’ support, a special ‘post-thumbnail’ image size is registered, which differs from the ‘thumbnail’ image size managed via the Settings > Media screen.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

When using the_post_thumbnail() or 相关函数 functions, the ‘post-thumbnail’ image size is used by default, though a different size can be specified instead as needed.


参数

$post

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

Default value: null

$size

(string|array)
(Optional)
Image size to use. Accepts any valid image size, 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.


源代码

File: wp-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
	 *
	 * @param string|array $size The post thumbnail size. Image size or array of width and height
	 *                           values (in that order). Default 'post-thumbnail'.
	 */
	$size = apply_filters( 'post_thumbnail_size', $size );

	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 string       $post_thumbnail_id The post thumbnail ID.
		 * @param string|array $size              The post thumbnail size. Image size or array of width
		 *                                        and height values (in that order). Default 'post-thumbnail'.
		 */
		do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
		if ( in_the_loop() )
			update_post_thumbnail_cache();
		$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 string       $post_thumbnail_id The post thumbnail ID.
		 * @param string|array $size              The post thumbnail size. Image size or array of width
		 *                                        and height values (in that order). Default 'post-thumbnail'.
		 */
		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 string       $post_thumbnail_id The post thumbnail ID.
	 * @param string|array $size              The post thumbnail size. Image size or array of width and height
	 *                                        values (in that order). Default 'post-thumbnail'.
	 * @param string       $attr              Query string of attributes.
	 */
	return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
}

更新日志

Version 描述
4.4.0 $post can be a post ID or WP_Post object.
2.9.0 Introduced.

More Information

If the required add_theme_support( 'post-thumbnails' ); in the current theme’s functions.php file is attached to a hook, it must be must be called before the init hook is fired. The init hook may be too late for some features. If attached to a hook, it must be after_setup_theme.


相关函数

Uses

  • wp-includes/query.php:
    in_the_loop()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/plugin.php:
    do_action()
  • wp-includes/post-thumbnail-template.php:
    get_post_thumbnail_id()
  • wp-includes/post-thumbnail-template.php:
    update_post_thumbnail_cache()
  • wp-includes/post-thumbnail-template.php:
    post_thumbnail_size
  • wp-includes/post-thumbnail-template.php:
    begin_fetch_post_thumbnail_html
  • wp-includes/post-thumbnail-template.php:
    end_fetch_post_thumbnail_html
  • wp-includes/post-thumbnail-template.php:
    post_thumbnail_html
  • wp-includes/media.php:
    wp_get_attachment_image()
  • wp-includes/post.php:
    get_post()
  • Show 6 more uses


Used By

  • wp-includes/post-thumbnail-template.php:
    the_post_thumbnail()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 7You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Styling Post Thumbnails
    Post Thumbnails are given a class “wp-post-image”. They also get a class depending on the size of the thumbnail being displayed. You can style the output with these CSS selectors:

    
    	img.wp-post-image
    	img.attachment-thumbnail
    	img.attachment-medium
    	img.attachment-large
    	img.attachment-full
    

    You can also give Post Thumbnails their own class:

    
    // Give the Post Thumbnail a class "alignleft".
    echo get_the_post_thumbnail( $post_id, 'thumbnail', array( 'class' => 'alignleft' ) );
    
  2. Sample Usage

    
    <?php $pages = get_pages( array( 'child_of' => 1 ) ); ?> 
    <ul>
    	<?php foreach ( $pages as $page ) : ?>
    		<li>
    			<?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
    			<h1><?php echo apply_filters( 'the_title', $page->post_title, $page->ID ); ?></h1>
    			<?php echo apply_filters( 'the_content', $page->post_content ); ?>
    		</li>
    	<?php endforeach; ?>
    </ul>
    

    Post Thumbnail Linking to the Post Permalink

    This example shows the 5 latest Post Thumbnails linked to their Post permalink.

    
    $posts = get_posts( array( 'posts_per_page' => 5 ) );
    foreach ( $posts as $_post ) {
    	if ( has_post_thumbnail( $_post->ID ) ) {
    		echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
    		echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
    		echo '</a>';
    	}
    }
    

    Thumbnail Sizes
    The default image sizes of WordPress are “thumbnail”, “medium”, “large” and “full” (the size of the image you uploaded).
    These image sizes can be configured in the WordPress Administration Media panel under Settings > Media.
    Themes may also add “post-thumbnail”. This is how you can use these default sizes with get_the_post_thumbnail():

    
    // without parameter -> Post Thumbnail (as set by theme using set_post_thumbnail_size())
    get_the_post_thumbnail( $post_id );                   
    
    get_the_post_thumbnail( $post_id, 'thumbnail' );      // Thumbnail (Note: different to Post Thumbnail)
    get_the_post_thumbnail( $post_id, 'medium' );         // Medium resolution
    get_the_post_thumbnail( $post_id, 'large' );          // Large resolution
    get_the_post_thumbnail( $post_id, 'full' );           // Original resolution
    
    get_the_post_thumbnail( $post_id, array( 100, 100) ); // Other resolutions
    

    Register new image sizes for Post Thumbnails with: add_image_size().
    To set the default size for Post Thumbnails see: set_post_thumbnail_size().

    Post Thumbnail Linking to large Image Size
    This example links to the “large” Post Thumbnail image size and must be used within The Loop.

    
    if ( has_post_thumbnail() ) {
    	$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
    	if ( ! empty( $large_image_url[0] ) ) {
    		echo '<a href="' . esc_url( $large_image_url[0] ) . '" title="' . the_title_attribute( array( 'echo' => 0 ) ) . '">';
    		echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); 
    		echo '</a>';
    	}
    }
    

    Remove width and height attributes from the returning html

    get_the_post_thumbnail function produces html with img tag which includes width, height, src, class, alt and title attributes. The following code snippet is useful if someone wish to remove width and height attribute.

    This function provides 'post_thumbnail_html' filter before returning the html.

    
    add_filter( 'post_thumbnail_html', 'remove_thumbnail_width_height', 10, 5 );
    
    function remove_thumbnail_width_height( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
        $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
        return $html;
    }
    

    See Also:
    the_post_thumbnail()

    Post thumbnails in a listing context are often linked to the content they’re representing. Instead of adding that every time, here is a filter you can add that will do it automatically based on an `$attr` passed in.

    
    /**
     * 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 wpdev_filter_post_thumbnail_html( $html, $pid, $post_thumbnail_id, $size, $attr ) {
    
    	 if ( ! empty( $attr[ 'link_thumbnail' ] ) ) {
    
    		$html = sprintf(
    			'<a href="%s" title="%s" rel="nofollow">%s</a>',
    			get_permalink( $pid ),
    			esc_attr( get_the_title( $pid ) ),
    			$html
    		);
    	 }
    
    	return $html;
    }
    
    add_filter( 'post_thumbnail_html', 'wpdev_filter_post_thumbnail_html', 10, 5 );
    

    Just “add the above to your functions.php file” (there’s probably a better place for it) and use it like this in a template file:

    
     'my-custom-class', // classes
    		'link_thumbnail' => TRUE, // yes please link my thumbnail
    	]
    );
    ?>
    

    To give class to the featured image post thumbnail

    
    // Give the Post_Thumbnail a class "alignleft".
    echo get_the_post_thumbnail( $post_id, 'post_thumbnail', array( 'class' =>; 'alignleft' ) );
    

    To get full featured image

    the_post_thumbnail( 'large','style=max-width:100%;height:auto;');

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈