get_the_post_thumbnail

Retrieve the post thumbnail.

Description

(string) get_the_post_thumbnail( (constant) $post = null, (string) $size = 'post-thumbnail', (string) $attr = '' ); 

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.

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

Returns (string)

The post thumbnail image tag.

Parameters (3)

0. $post — Optional. (constant) => null
Post ID or WP_Post object. Default is global $post..
1. $size — Optional. (string) => 'post-thumbnail'
Image size to use. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default post-thumbnail..
2. $attr — Optional. (string) => ''
Query string or array of attributes. Default empty.

Usage

  1. if ( !function_exists( 'get_the_post_thumbnail' ) ) { 
  2. require_once ABSPATH . WPINC . '/post-thumbnail-template.php'; 
  3.  
  4. // Optional. Post ID or WP_Post object. Default is global `$post`. 
  5. $post = null; 
  6. $size = 'post-thumbnail'; 
  7.  
  8. // Optional. Query string or array of attributes. Default empty. 
  9. $attr = ''; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = get_the_post_thumbnail($post, $size, $attr); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post-thumbnail-template.php  
  1. function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) { 
  2. $post = get_post( $post ); 
  3. if ( ! $post ) { 
  4. return ''; 
  5. $post_thumbnail_id = get_post_thumbnail_id( $post ); 
  6.  
  7. /** 
  8. * Filters the post thumbnail size. 
  9. * @since 2.9.0 
  10. * @param string|array $size The post thumbnail size. Image size or array of width and height 
  11. * values (in that order). Default 'post-thumbnail'. 
  12. */ 
  13. $size = apply_filters( 'post_thumbnail_size', $size ); 
  14.  
  15. if ( $post_thumbnail_id ) { 
  16.  
  17. /** 
  18. * Fires before fetching the post thumbnail HTML. 
  19. * Provides "just in time" filtering of all filters in wp_get_attachment_image(). 
  20. * @since 2.9.0 
  21. * @param int $post_id The post ID. 
  22. * @param string $post_thumbnail_id The post thumbnail ID. 
  23. * @param string|array $size The post thumbnail size. Image size or array of width 
  24. * and height values (in that order). Default 'post-thumbnail'. 
  25. */ 
  26. do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size ); 
  27. if ( in_the_loop() ) 
  28. $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); 
  29.  
  30. /** 
  31. * Fires after fetching the post thumbnail HTML. 
  32. * @since 2.9.0 
  33. * @param int $post_id The post ID. 
  34. * @param string $post_thumbnail_id The post thumbnail ID. 
  35. * @param string|array $size The post thumbnail size. Image size or array of width 
  36. * and height values (in that order). Default 'post-thumbnail'. 
  37. */ 
  38. do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size ); 
  39.  
  40. } else { 
  41. $html = ''; 
  42. /** 
  43. * Filters the post thumbnail HTML. 
  44. * @since 2.9.0 
  45. * @param string $html The post thumbnail HTML. 
  46. * @param int $post_id The post ID. 
  47. * @param string $post_thumbnail_id The post thumbnail ID. 
  48. * @param string|array $size The post thumbnail size. Image size or array of width and height 
  49. * values (in that order). Default 'post-thumbnail'. 
  50. * @param string $attr Query string of attributes. 
  51. */ 
  52. return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );