9 * @subpackage Template |
9 * @subpackage Template |
10 */ |
10 */ |
11 |
11 |
12 /** |
12 /** |
13 * Check if post has an image attached. |
13 * Check if post has an image attached. |
14 * |
14 * |
15 * @since 2.9.0 |
15 * @since 2.9.0 |
16 * |
16 * |
17 * @param int $post_id Optional. Post ID. |
17 * @param int $post_id Optional. Post ID. |
18 * @return bool Whether post has an image attached (true) or not (false). |
18 * @return bool Whether post has an image attached. |
19 */ |
19 */ |
20 function has_post_thumbnail( $post_id = NULL ) { |
20 function has_post_thumbnail( $post_id = null ) { |
21 global $id; |
21 return (bool) get_post_thumbnail_id( $post_id ); |
22 $post_id = ( NULL === $post_id ) ? $id : $post_id; |
|
23 return !! get_post_thumbnail_id( $post_id ); |
|
24 } |
22 } |
25 |
23 |
26 /** |
24 /** |
27 * Retrieve Post Thumbnail ID. |
25 * Retrieve Post Thumbnail ID. |
28 * |
26 * |
29 * @since 2.9.0 |
27 * @since 2.9.0 |
30 * |
28 * |
31 * @param int $post_id Optional. Post ID. |
29 * @param int $post_id Optional. Post ID. |
32 * @return int |
30 * @return int |
33 */ |
31 */ |
34 function get_post_thumbnail_id( $post_id = NULL ) { |
32 function get_post_thumbnail_id( $post_id = null ) { |
35 global $id; |
33 $post_id = ( null === $post_id ) ? get_the_ID() : $post_id; |
36 $post_id = ( NULL === $post_id ) ? $id : $post_id; |
|
37 return get_post_meta( $post_id, '_thumbnail_id', true ); |
34 return get_post_meta( $post_id, '_thumbnail_id', true ); |
38 } |
35 } |
39 |
36 |
40 /** |
37 /** |
41 * Display Post Thumbnail. |
38 * Display Post Thumbnail. |
42 * |
39 * |
43 * @since 2.9.0 |
40 * @since 2.9.0 |
44 * |
41 * |
45 * @param int $size Optional. Image size. Defaults to 'post-thumbnail', which theme sets using set_post_thumbnail_size( $width, $height, $crop_flag );. |
42 * @param int $size Optional. Image size. Defaults to 'post-thumbnail', which theme sets using set_post_thumbnail_size( $width, $height, $crop_flag );. |
46 * @param string|array $attr Optional. Query string or array of attributes. |
43 * @param string|array $attr Optional. Query string or array of attributes. |
47 */ |
44 */ |
48 function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { |
45 function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { |
49 echo get_the_post_thumbnail( NULL, $size, $attr ); |
46 echo get_the_post_thumbnail( null, $size, $attr ); |
|
47 } |
|
48 |
|
49 /** |
|
50 * Update cache for thumbnails in the current loop |
|
51 * |
|
52 * @since 3.2 |
|
53 * |
|
54 * @param object $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global. |
|
55 */ |
|
56 function update_post_thumbnail_cache( $wp_query = null ) { |
|
57 if ( ! $wp_query ) |
|
58 $wp_query = $GLOBALS['wp_query']; |
|
59 |
|
60 if ( $wp_query->thumbnails_cached ) |
|
61 return; |
|
62 |
|
63 $thumb_ids = array(); |
|
64 foreach ( $wp_query->posts as $post ) { |
|
65 if ( $id = get_post_thumbnail_id( $post->ID ) ) |
|
66 $thumb_ids[] = $id; |
|
67 } |
|
68 |
|
69 if ( ! empty ( $thumb_ids ) ) { |
|
70 _prime_post_caches( $thumb_ids, false, true ); |
|
71 } |
|
72 |
|
73 $wp_query->thumbnails_cached = true; |
50 } |
74 } |
51 |
75 |
52 /** |
76 /** |
53 * Retrieve Post Thumbnail. |
77 * Retrieve Post Thumbnail. |
54 * |
78 * |
55 * @since 2.9.0 |
79 * @since 2.9.0 |
56 * |
80 * |
57 * @param int $post_id Optional. Post ID. |
81 * @param int $post_id Optional. Post ID. |
58 * @param string $size Optional. Image size. Defaults to 'thumbnail'. |
82 * @param string $size Optional. Image size. Defaults to 'post-thumbnail'. |
59 * @param string|array $attr Optional. Query string or array of attributes. |
83 * @param string|array $attr Optional. Query string or array of attributes. |
60 */ |
84 */ |
61 function get_the_post_thumbnail( $post_id = NULL, $size = 'post-thumbnail', $attr = '' ) { |
85 function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' ) { |
62 global $id; |
86 $post_id = ( null === $post_id ) ? get_the_ID() : $post_id; |
63 $post_id = ( NULL === $post_id ) ? $id : $post_id; |
|
64 $post_thumbnail_id = get_post_thumbnail_id( $post_id ); |
87 $post_thumbnail_id = get_post_thumbnail_id( $post_id ); |
65 $size = apply_filters( 'post_thumbnail_size', $size ); |
88 $size = apply_filters( 'post_thumbnail_size', $size ); |
66 if ( $post_thumbnail_id ) { |
89 if ( $post_thumbnail_id ) { |
67 do_action( 'begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size ); // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters |
90 do_action( 'begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size ); // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters |
|
91 if ( in_the_loop() ) |
|
92 update_post_thumbnail_cache(); |
68 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); |
93 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); |
69 do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size ); |
94 do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size ); |
70 } else { |
95 } else { |
71 $html = ''; |
96 $html = ''; |
72 } |
97 } |
73 return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr ); |
98 return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr ); |
74 } |
99 } |
75 |
|
76 ?> |
|