|
0
|
1 |
<?php |
|
|
2 |
/** |
|
|
3 |
* WordPress Post Thumbnail Template Functions. |
|
|
4 |
* |
|
5
|
5 |
* Support for post thumbnails. |
|
|
6 |
* Theme's functions.php must call add_theme_support( 'post-thumbnails' ) to use these. |
|
0
|
7 |
* |
|
|
8 |
* @package WordPress |
|
|
9 |
* @subpackage Template |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
/** |
|
|
13 |
* Check if post has an image attached. |
|
|
14 |
* |
|
|
15 |
* @since 2.9.0 |
|
|
16 |
* |
|
|
17 |
* @param int $post_id Optional. Post ID. |
|
|
18 |
* @return bool Whether post has an image attached. |
|
|
19 |
*/ |
|
|
20 |
function has_post_thumbnail( $post_id = null ) { |
|
|
21 |
return (bool) get_post_thumbnail_id( $post_id ); |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
/** |
|
|
25 |
* Retrieve Post Thumbnail ID. |
|
|
26 |
* |
|
|
27 |
* @since 2.9.0 |
|
|
28 |
* |
|
|
29 |
* @param int $post_id Optional. Post ID. |
|
|
30 |
* @return int |
|
|
31 |
*/ |
|
|
32 |
function get_post_thumbnail_id( $post_id = null ) { |
|
|
33 |
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id; |
|
|
34 |
return get_post_meta( $post_id, '_thumbnail_id', true ); |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
/** |
|
5
|
38 |
* Display the post thumbnail. |
|
|
39 |
* |
|
|
40 |
* When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
|
|
41 |
* is registered, which differs from the 'thumbnail' image size managed via the |
|
|
42 |
* Settings > Media screen. |
|
|
43 |
* |
|
|
44 |
* When using the_post_thumbnail() or related functions, the 'post-thumbnail' image |
|
|
45 |
* size is used by default, though a different size can be specified instead as needed. |
|
0
|
46 |
* |
|
|
47 |
* @since 2.9.0 |
|
|
48 |
* |
|
5
|
49 |
* @see get_the_post_thumbnail() |
|
|
50 |
* |
|
|
51 |
* @param string|array $size Optional. Registered image size to use, or flat array of height |
|
|
52 |
* and width values. Default 'post-thumbnail'. |
|
|
53 |
* @param string|array $attr Optional. Query string or array of attributes. Default empty. |
|
0
|
54 |
*/ |
|
|
55 |
function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { |
|
|
56 |
echo get_the_post_thumbnail( null, $size, $attr ); |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
/** |
|
|
60 |
* Update cache for thumbnails in the current loop |
|
|
61 |
* |
|
5
|
62 |
* @since 3.2.0 |
|
0
|
63 |
* |
|
|
64 |
* @param object $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global. |
|
|
65 |
*/ |
|
|
66 |
function update_post_thumbnail_cache( $wp_query = null ) { |
|
|
67 |
if ( ! $wp_query ) |
|
|
68 |
$wp_query = $GLOBALS['wp_query']; |
|
|
69 |
|
|
|
70 |
if ( $wp_query->thumbnails_cached ) |
|
|
71 |
return; |
|
|
72 |
|
|
|
73 |
$thumb_ids = array(); |
|
|
74 |
foreach ( $wp_query->posts as $post ) { |
|
|
75 |
if ( $id = get_post_thumbnail_id( $post->ID ) ) |
|
|
76 |
$thumb_ids[] = $id; |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
if ( ! empty ( $thumb_ids ) ) { |
|
|
80 |
_prime_post_caches( $thumb_ids, false, true ); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
$wp_query->thumbnails_cached = true; |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
/** |
|
5
|
87 |
* Retrieve the post thumbnail. |
|
|
88 |
* |
|
|
89 |
* When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
|
|
90 |
* is registered, which differs from the 'thumbnail' image size managed via the |
|
|
91 |
* Settings > Media screen. |
|
|
92 |
* |
|
|
93 |
* When using the_post_thumbnail() or related functions, the 'post-thumbnail' image |
|
|
94 |
* size is used by default, though a different size can be specified instead as needed. |
|
0
|
95 |
* |
|
|
96 |
* @since 2.9.0 |
|
|
97 |
* |
|
5
|
98 |
* @param int $post_id Post ID. Default is the ID of the `$post` global. |
|
|
99 |
* @param string|array $size Optional. Registered image size to use, or flat array of height |
|
|
100 |
* and width values. Default 'post-thumbnail'. |
|
|
101 |
* @param string|array $attr Optional. Query string or array of attributes. Default empty. |
|
0
|
102 |
*/ |
|
|
103 |
function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' ) { |
|
|
104 |
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id; |
|
|
105 |
$post_thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
|
106 |
|
|
|
107 |
/** |
|
|
108 |
* Filter the post thumbnail size. |
|
|
109 |
* |
|
|
110 |
* @since 2.9.0 |
|
|
111 |
* |
|
|
112 |
* @param string $size The post thumbnail size. |
|
|
113 |
*/ |
|
|
114 |
$size = apply_filters( 'post_thumbnail_size', $size ); |
|
|
115 |
|
|
|
116 |
if ( $post_thumbnail_id ) { |
|
|
117 |
|
|
|
118 |
/** |
|
|
119 |
* Fires before fetching the post thumbnail HTML. |
|
|
120 |
* |
|
|
121 |
* Provides "just in time" filtering of all filters in wp_get_attachment_image(). |
|
|
122 |
* |
|
|
123 |
* @since 2.9.0 |
|
|
124 |
* |
|
|
125 |
* @param string $post_id The post ID. |
|
|
126 |
* @param string $post_thumbnail_id The post thumbnail ID. |
|
|
127 |
* @param string $size The post thumbnail size. |
|
|
128 |
*/ |
|
|
129 |
do_action( 'begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size ); |
|
|
130 |
if ( in_the_loop() ) |
|
|
131 |
update_post_thumbnail_cache(); |
|
|
132 |
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); |
|
|
133 |
|
|
|
134 |
/** |
|
|
135 |
* Fires after fetching the post thumbnail HTML. |
|
|
136 |
* |
|
|
137 |
* @since 2.9.0 |
|
|
138 |
* |
|
|
139 |
* @param string $post_id The post ID. |
|
|
140 |
* @param string $post_thumbnail_id The post thumbnail ID. |
|
|
141 |
* @param string $size The post thumbnail size. |
|
|
142 |
*/ |
|
|
143 |
do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size ); |
|
|
144 |
|
|
|
145 |
} else { |
|
|
146 |
$html = ''; |
|
|
147 |
} |
|
|
148 |
/** |
|
|
149 |
* Filter the post thumbnail HTML. |
|
|
150 |
* |
|
|
151 |
* @since 2.9.0 |
|
|
152 |
* |
|
|
153 |
* @param string $html The post thumbnail HTML. |
|
|
154 |
* @param string $post_id The post ID. |
|
|
155 |
* @param string $post_thumbnail_id The post thumbnail ID. |
|
|
156 |
* @param string $size The post thumbnail size. |
|
|
157 |
* @param string $attr Query string of attributes. |
|
|
158 |
*/ |
|
|
159 |
return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr ); |
|
|
160 |
} |