post-thumbnail-template.php 8.0 KB

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