post-thumbnail-template.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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|false $thumbnail_id Post thumbnail ID or false if the post does not exist.
  35. */
  36. return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
  37. }
  38. /**
  39. * Retrieves the 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. * @since 5.5.0 The return value for a non-existing post
  44. * was changed to false instead of an empty string.
  45. *
  46. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  47. * @return int|false Post thumbnail ID (which can be 0 if the thumbnail is not set),
  48. * or false if the post does not exist.
  49. */
  50. function get_post_thumbnail_id( $post = null ) {
  51. $post = get_post( $post );
  52. if ( ! $post ) {
  53. return false;
  54. }
  55. $thumbnail_id = (int) get_post_meta( $post->ID, '_thumbnail_id', true );
  56. /**
  57. * Filters the post thumbnail ID.
  58. *
  59. * @since 5.9.0
  60. *
  61. * @param int|false $thumbnail_id Post thumbnail ID or false if the post does not exist.
  62. * @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`.
  63. */
  64. return (int) apply_filters( 'post_thumbnail_id', $thumbnail_id, $post );
  65. }
  66. /**
  67. * Displays the post thumbnail.
  68. *
  69. * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
  70. * is registered, which differs from the 'thumbnail' image size managed via the
  71. * Settings > Media screen.
  72. *
  73. * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
  74. * size is used by default, though a different size can be specified instead as needed.
  75. *
  76. * @since 2.9.0
  77. *
  78. * @see get_the_post_thumbnail()
  79. *
  80. * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
  81. * width and height values in pixels (in that order). Default 'post-thumbnail'.
  82. * @param string|array $attr Optional. Query string or array of attributes. Default empty.
  83. */
  84. function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
  85. echo get_the_post_thumbnail( null, $size, $attr );
  86. }
  87. /**
  88. * Updates cache for thumbnails in the current loop.
  89. *
  90. * @since 3.2.0
  91. *
  92. * @global WP_Query $wp_query WordPress Query object.
  93. *
  94. * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
  95. */
  96. function update_post_thumbnail_cache( $wp_query = null ) {
  97. if ( ! $wp_query ) {
  98. $wp_query = $GLOBALS['wp_query'];
  99. }
  100. if ( $wp_query->thumbnails_cached ) {
  101. return;
  102. }
  103. $thumb_ids = array();
  104. foreach ( $wp_query->posts as $post ) {
  105. $id = get_post_thumbnail_id( $post->ID );
  106. if ( $id ) {
  107. $thumb_ids[] = $id;
  108. }
  109. }
  110. if ( ! empty( $thumb_ids ) ) {
  111. _prime_post_caches( $thumb_ids, false, true );
  112. }
  113. $wp_query->thumbnails_cached = true;
  114. }
  115. /**
  116. * Retrieves the post thumbnail.
  117. *
  118. * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
  119. * is registered, which differs from the 'thumbnail' image size managed via the
  120. * Settings > Media screen.
  121. *
  122. * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
  123. * size is used by default, though a different size can be specified instead as needed.
  124. *
  125. * @since 2.9.0
  126. * @since 4.4.0 `$post` can be a post ID or WP_Post object.
  127. *
  128. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  129. * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
  130. * width and height values in pixels (in that order). Default 'post-thumbnail'.
  131. * @param string|array $attr Optional. Query string or array of attributes. Default empty.
  132. * @return string The post thumbnail image tag.
  133. */
  134. function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
  135. $post = get_post( $post );
  136. if ( ! $post ) {
  137. return '';
  138. }
  139. $post_thumbnail_id = get_post_thumbnail_id( $post );
  140. /**
  141. * Filters the post thumbnail size.
  142. *
  143. * @since 2.9.0
  144. * @since 4.9.0 Added the `$post_id` parameter.
  145. *
  146. * @param string|int[] $size Requested image size. Can be any registered image size name, or
  147. * an array of width and height values in pixels (in that order).
  148. * @param int $post_id The post ID.
  149. */
  150. $size = apply_filters( 'post_thumbnail_size', $size, $post->ID );
  151. if ( $post_thumbnail_id ) {
  152. /**
  153. * Fires before fetching the post thumbnail HTML.
  154. *
  155. * Provides "just in time" filtering of all filters in wp_get_attachment_image().
  156. *
  157. * @since 2.9.0
  158. *
  159. * @param int $post_id The post ID.
  160. * @param int $post_thumbnail_id The post thumbnail ID.
  161. * @param string|int[] $size Requested image size. Can be any registered image size name, or
  162. * an array of width and height values in pixels (in that order).
  163. */
  164. do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
  165. if ( in_the_loop() ) {
  166. update_post_thumbnail_cache();
  167. }
  168. // Get the 'loading' attribute value to use as default, taking precedence over the default from
  169. // `wp_get_attachment_image()`.
  170. $loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
  171. // Add the default to the given attributes unless they already include a 'loading' directive.
  172. if ( empty( $attr ) ) {
  173. $attr = array( 'loading' => $loading );
  174. } elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
  175. $attr['loading'] = $loading;
  176. } elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
  177. $attr .= '&loading=' . $loading;
  178. }
  179. $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
  180. /**
  181. * Fires after fetching the post thumbnail HTML.
  182. *
  183. * @since 2.9.0
  184. *
  185. * @param int $post_id The post ID.
  186. * @param int $post_thumbnail_id The post thumbnail ID.
  187. * @param string|int[] $size Requested image size. Can be any registered image size name, or
  188. * an array of width and height values in pixels (in that order).
  189. */
  190. do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
  191. } else {
  192. $html = '';
  193. }
  194. /**
  195. * Filters the post thumbnail HTML.
  196. *
  197. * @since 2.9.0
  198. *
  199. * @param string $html The post thumbnail HTML.
  200. * @param int $post_id The post ID.
  201. * @param int $post_thumbnail_id The post thumbnail ID, or 0 if there isn't one.
  202. * @param string|int[] $size Requested image size. Can be any registered image size name, or
  203. * an array of width and height values in pixels (in that order).
  204. * @param string|array $attr Query string or array of attributes.
  205. */
  206. return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
  207. }
  208. /**
  209. * Returns the post thumbnail URL.
  210. *
  211. * @since 4.4.0
  212. *
  213. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  214. * @param string|int[] $size Optional. Registered image size to retrieve the source for or a flat array
  215. * of height and width dimensions. Default 'post-thumbnail'.
  216. * @return string|false Post thumbnail URL or false if no image is available. If `$size` does not match
  217. * any registered image size, the original image URL will be returned.
  218. */
  219. function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
  220. $post_thumbnail_id = get_post_thumbnail_id( $post );
  221. if ( ! $post_thumbnail_id ) {
  222. return false;
  223. }
  224. $thumbnail_url = wp_get_attachment_image_url( $post_thumbnail_id, $size );
  225. /**
  226. * Filters the post thumbnail URL.
  227. *
  228. * @since 5.9.0
  229. *
  230. * @param string|false $thumbnail_url Post thumbnail URL or false if the post does not exist.
  231. * @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`.
  232. * @param string|int[] $size Registered image size to retrieve the source for or a flat array
  233. * of height and width dimensions. Default 'post-thumbnail'.
  234. */
  235. return apply_filters( 'post_thumbnail_url', $thumbnail_url, $post, $size );
  236. }
  237. /**
  238. * Displays the post thumbnail URL.
  239. *
  240. * @since 4.4.0
  241. *
  242. * @param string|int[] $size Optional. Image size to use. Accepts any valid image size,
  243. * or an array of width and height values in pixels (in that order).
  244. * Default 'post-thumbnail'.
  245. */
  246. function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
  247. $url = get_the_post_thumbnail_url( null, $size );
  248. if ( $url ) {
  249. echo esc_url( $url );
  250. }
  251. }
  252. /**
  253. * Returns the post thumbnail caption.
  254. *
  255. * @since 4.6.0
  256. *
  257. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  258. * @return string Post thumbnail caption.
  259. */
  260. function get_the_post_thumbnail_caption( $post = null ) {
  261. $post_thumbnail_id = get_post_thumbnail_id( $post );
  262. if ( ! $post_thumbnail_id ) {
  263. return '';
  264. }
  265. $caption = wp_get_attachment_caption( $post_thumbnail_id );
  266. if ( ! $caption ) {
  267. $caption = '';
  268. }
  269. return $caption;
  270. }
  271. /**
  272. * Displays the post thumbnail caption.
  273. *
  274. * @since 4.6.0
  275. *
  276. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  277. */
  278. function the_post_thumbnail_caption( $post = null ) {
  279. /**
  280. * Filters the displayed post thumbnail caption.
  281. *
  282. * @since 4.6.0
  283. *
  284. * @param string $caption Caption for the given attachment.
  285. */
  286. echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) );
  287. }