You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

254 lines
7.9 KiB

  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. *
  116. * @param string|array $size The post thumbnail size. Image size or array of width and height
  117. * values (in that order). Default 'post-thumbnail'.
  118. */
  119. $size = apply_filters( 'post_thumbnail_size', $size );
  120. if ( $post_thumbnail_id ) {
  121. /**
  122. * Fires before fetching the post thumbnail HTML.
  123. *
  124. * Provides "just in time" filtering of all filters in wp_get_attachment_image().
  125. *
  126. * @since 2.9.0
  127. *
  128. * @param int $post_id The post ID.
  129. * @param string $post_thumbnail_id The post thumbnail ID.
  130. * @param string|array $size The post thumbnail size. Image size or array of width
  131. * and height values (in that order). Default 'post-thumbnail'.
  132. */
  133. do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
  134. if ( in_the_loop() )
  135. update_post_thumbnail_cache();
  136. $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
  137. /**
  138. * Fires after fetching the post thumbnail HTML.
  139. *
  140. * @since 2.9.0
  141. *
  142. * @param int $post_id The post ID.
  143. * @param string $post_thumbnail_id The post thumbnail ID.
  144. * @param string|array $size The post thumbnail size. Image size or array of width
  145. * and height values (in that order). Default 'post-thumbnail'.
  146. */
  147. do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
  148. } else {
  149. $html = '';
  150. }
  151. /**
  152. * Filters the post thumbnail HTML.
  153. *
  154. * @since 2.9.0
  155. *
  156. * @param string $html The post thumbnail HTML.
  157. * @param int $post_id The post ID.
  158. * @param string $post_thumbnail_id The post thumbnail ID.
  159. * @param string|array $size The post thumbnail size. Image size or array of width and height
  160. * values (in that order). Default 'post-thumbnail'.
  161. * @param string $attr Query string of attributes.
  162. */
  163. return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
  164. }
  165. /**
  166. * Return the post thumbnail URL.
  167. *
  168. * @since 4.4.0
  169. *
  170. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  171. * @param string|array $size Optional. Registered image size to retrieve the source for or a flat
  172. * array of height and width dimensions. Default 'post-thumbnail'.
  173. * @return string|false Post thumbnail URL or false if no URL is available.
  174. */
  175. function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
  176. $post_thumbnail_id = get_post_thumbnail_id( $post );
  177. if ( ! $post_thumbnail_id ) {
  178. return false;
  179. }
  180. return wp_get_attachment_image_url( $post_thumbnail_id, $size );
  181. }
  182. /**
  183. * Display the post thumbnail URL.
  184. *
  185. * @since 4.4.0
  186. *
  187. * @param string|array $size Optional. Image size to use. Accepts any valid image size,
  188. * or an array of width and height values in pixels (in that order).
  189. * Default 'post-thumbnail'.
  190. */
  191. function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
  192. $url = get_the_post_thumbnail_url( null, $size );
  193. if ( $url ) {
  194. echo esc_url( $url );
  195. }
  196. }
  197. /**
  198. * Returns the post thumbnail caption.
  199. *
  200. * @since 4.6.0
  201. *
  202. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  203. * @return string Post thumbnail caption.
  204. */
  205. function get_the_post_thumbnail_caption( $post = null ) {
  206. $post_thumbnail_id = get_post_thumbnail_id( $post );
  207. if ( ! $post_thumbnail_id ) {
  208. return '';
  209. }
  210. $caption = wp_get_attachment_caption( $post_thumbnail_id );
  211. if ( ! $caption ) {
  212. $caption = '';
  213. }
  214. return $caption;
  215. }
  216. /**
  217. * Displays the post thumbnail caption.
  218. *
  219. * @since 4.6.0
  220. *
  221. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  222. */
  223. function the_post_thumbnail_caption( $post = null ) {
  224. /**
  225. * Filters the displayed post thumbnail caption.
  226. *
  227. * @since 4.6.0
  228. *
  229. * @param string $caption Caption for the given attachment.
  230. */
  231. echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) );
  232. }