post-thumbnail-template.php 8.8 KB

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