/wp-includes/post-thumbnail-template.php

  1. <?php 
  2. /** 
  3. * WordPress Post Thumbnail Template Functions. 
  4. * 
  5. * Support for post thumbnails. 
  6. * Theme's functions.php must call add_theme_support( 'post-thumbnails' ) to use these. 
  7. * 
  8. * @package WordPress 
  9. * @subpackage Template 
  10. */ 
  11.  
  12. /** 
  13. * Check if post has an image attached. 
  14. * 
  15. * @since 2.9.0 
  16. * @since 4.4.0 `$post` can be a post ID or WP_Post object. 
  17. * 
  18. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 
  19. * @return bool Whether the post has an image attached. 
  20. */ 
  21. function has_post_thumbnail( $post = null ) { 
  22. return (bool) get_post_thumbnail_id( $post ); 
  23.  
  24. /** 
  25. * Retrieve post thumbnail ID. 
  26. * 
  27. * @since 2.9.0 
  28. * @since 4.4.0 `$post` can be a post ID or WP_Post object. 
  29. * 
  30. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 
  31. * @return string|int Post thumbnail ID or empty string. 
  32. */ 
  33. function get_post_thumbnail_id( $post = null ) { 
  34. $post = get_post( $post ); 
  35. if ( ! $post ) { 
  36. return ''; 
  37. return get_post_meta( $post->ID, '_thumbnail_id', true ); 
  38.  
  39. /** 
  40. * Display the post thumbnail. 
  41. * 
  42. * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size 
  43. * is registered, which differs from the 'thumbnail' image size managed via the 
  44. * Settings > Media screen. 
  45. * 
  46. * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image 
  47. * size is used by default, though a different size can be specified instead as needed. 
  48. * 
  49. * @since 2.9.0 
  50. * 
  51. * @see get_the_post_thumbnail() 
  52. * 
  53. * @param string|array $size Optional. Image size to use. Accepts any valid image size, or 
  54. * an array of width and height values in pixels (in that order). 
  55. * Default 'post-thumbnail'. 
  56. * @param string|array $attr Optional. Query string or array of attributes. Default empty. 
  57. */ 
  58. function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { 
  59. echo get_the_post_thumbnail( null, $size, $attr ); 
  60.  
  61. /** 
  62. * Update cache for thumbnails in the current loop. 
  63. * 
  64. * @since 3.2.0 
  65. * 
  66. * @global WP_Query $wp_query 
  67. * 
  68. * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global. 
  69. */ 
  70. function update_post_thumbnail_cache( $wp_query = null ) { 
  71. if ( ! $wp_query ) 
  72. $wp_query = $GLOBALS['wp_query']; 
  73.  
  74. if ( $wp_query->thumbnails_cached ) 
  75. return; 
  76.  
  77. $thumb_ids = array(); 
  78. foreach ( $wp_query->posts as $post ) { 
  79. if ( $id = get_post_thumbnail_id( $post->ID ) ) 
  80. $thumb_ids[] = $id; 
  81.  
  82. if ( ! empty ( $thumb_ids ) ) { 
  83. _prime_post_caches( $thumb_ids, false, true ); 
  84.  
  85. $wp_query->thumbnails_cached = true; 
  86.  
  87. /** 
  88. * Retrieve the post thumbnail. 
  89. * 
  90. * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size 
  91. * is registered, which differs from the 'thumbnail' image size managed via the 
  92. * Settings > Media screen. 
  93. * 
  94. * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image 
  95. * size is used by default, though a different size can be specified instead as needed. 
  96. * 
  97. * @since 2.9.0 
  98. * @since 4.4.0 `$post` can be a post ID or WP_Post object. 
  99. * 
  100. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 
  101. * @param string|array $size Optional. Image size to use. Accepts any valid image size, or 
  102. * an array of width and height values in pixels (in that order). 
  103. * Default 'post-thumbnail'. 
  104. * @param string|array $attr Optional. Query string or array of attributes. Default empty. 
  105. * @return string The post thumbnail image tag. 
  106. */ 
  107. function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) { 
  108. $post = get_post( $post ); 
  109. if ( ! $post ) { 
  110. return ''; 
  111. $post_thumbnail_id = get_post_thumbnail_id( $post ); 
  112.  
  113. /** 
  114. * Filters the post thumbnail size. 
  115. * 
  116. * @since 2.9.0 
  117. * 
  118. * @param string|array $size The post thumbnail size. Image size or array of width and height 
  119. * values (in that order). Default 'post-thumbnail'. 
  120. */ 
  121. $size = apply_filters( 'post_thumbnail_size', $size ); 
  122.  
  123. if ( $post_thumbnail_id ) { 
  124.  
  125. /** 
  126. * Fires before fetching the post thumbnail HTML. 
  127. * 
  128. * Provides "just in time" filtering of all filters in wp_get_attachment_image(). 
  129. * 
  130. * @since 2.9.0 
  131. * 
  132. * @param int $post_id The post ID. 
  133. * @param string $post_thumbnail_id The post thumbnail ID. 
  134. * @param string|array $size The post thumbnail size. Image size or array of width 
  135. * and height values (in that order). Default 'post-thumbnail'. 
  136. */ 
  137. do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size ); 
  138. if ( in_the_loop() ) 
  139. update_post_thumbnail_cache(); 
  140. $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); 
  141.  
  142. /** 
  143. * Fires after fetching the post thumbnail HTML. 
  144. * 
  145. * @since 2.9.0 
  146. * 
  147. * @param int $post_id The post ID. 
  148. * @param string $post_thumbnail_id The post thumbnail ID. 
  149. * @param string|array $size The post thumbnail size. Image size or array of width 
  150. * and height values (in that order). Default 'post-thumbnail'. 
  151. */ 
  152. do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size ); 
  153.  
  154. } else { 
  155. $html = ''; 
  156. /** 
  157. * Filters the post thumbnail HTML. 
  158. * 
  159. * @since 2.9.0 
  160. * 
  161. * @param string $html The post thumbnail HTML. 
  162. * @param int $post_id The post ID. 
  163. * @param string $post_thumbnail_id The post thumbnail ID. 
  164. * @param string|array $size The post thumbnail size. Image size or array of width and height 
  165. * values (in that order). Default 'post-thumbnail'. 
  166. * @param string $attr Query string of attributes. 
  167. */ 
  168. return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr ); 
  169.  
  170. /** 
  171. * Return the post thumbnail URL. 
  172. * 
  173. * @since 4.4.0 
  174. * 
  175. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 
  176. * @param string|array $size Optional. Registered image size to retrieve the source for or a flat 
  177. * array of height and width dimensions. Default 'post-thumbnail'. 
  178. * @return string|false Post thumbnail URL or false if no URL is available. 
  179. */ 
  180. function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) { 
  181. $post_thumbnail_id = get_post_thumbnail_id( $post ); 
  182. if ( ! $post_thumbnail_id ) { 
  183. return false; 
  184. return wp_get_attachment_image_url( $post_thumbnail_id, $size ); 
  185.  
  186. /** 
  187. * Display the post thumbnail URL. 
  188. * 
  189. * @since 4.4.0 
  190. * 
  191. * @param string|array $size Optional. Image size to use. Accepts any valid image size,  
  192. * or an array of width and height values in pixels (in that order). 
  193. * Default 'post-thumbnail'. 
  194. */ 
  195. function the_post_thumbnail_url( $size = 'post-thumbnail' ) { 
  196. $url = get_the_post_thumbnail_url( null, $size ); 
  197. if ( $url ) { 
  198. echo esc_url( $url ); 
  199.  
  200. /** 
  201. * Returns the post thumbnail caption. 
  202. * 
  203. * @since 4.6.0 
  204. * 
  205. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 
  206. * @return string Post thumbnail caption. 
  207. */ 
  208. function get_the_post_thumbnail_caption( $post = null ) { 
  209. $post_thumbnail_id = get_post_thumbnail_id( $post ); 
  210. if ( ! $post_thumbnail_id ) { 
  211. return ''; 
  212.  
  213. $caption = wp_get_attachment_caption( $post_thumbnail_id ); 
  214.  
  215. if ( ! $caption ) { 
  216. $caption = ''; 
  217.  
  218. return $caption; 
  219.  
  220. /** 
  221. * Displays the post thumbnail caption. 
  222. * 
  223. * @since 4.6.0 
  224. * 
  225. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 
  226. */ 
  227. function the_post_thumbnail_caption( $post = null ) { 
  228. /** 
  229. * Filters the displayed post thumbnail caption. 
  230. * 
  231. * @since 4.6.0 
  232. * 
  233. * @param string $caption Caption for the given attachment. 
  234. */ 
  235. echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) ); 
.