Nessuna descrizione

post-thumbnail-template.php 9.1KB

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