]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/post-thumbnail-template.php
WordPress 4.3.1
[autoinstalls/wordpress.git] / wp-includes / post-thumbnail-template.php
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 /**
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|null $post_id Optional. Post ID.
30  * @return mixed
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 /**
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.
46  *
47  * @since 2.9.0
48  *
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.
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  *
62  * @since 3.2.0
63  *
64  * @global WP_Query $wp_query
65  *
66  * @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
67  */
68 function update_post_thumbnail_cache( $wp_query = null ) {
69         if ( ! $wp_query )
70                 $wp_query = $GLOBALS['wp_query'];
71
72         if ( $wp_query->thumbnails_cached )
73                 return;
74
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
81         if ( ! empty ( $thumb_ids ) ) {
82                 _prime_post_caches( $thumb_ids, false, true );
83         }
84
85         $wp_query->thumbnails_cached = true;
86 }
87
88 /**
89  * Retrieve the post thumbnail.
90  *
91  * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
92  * is registered, which differs from the 'thumbnail' image size managed via the
93  * Settings > Media screen.
94  *
95  * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
96  * size is used by default, though a different size can be specified instead as needed.
97  *
98  * @since 2.9.0
99  *
100  * @param int $post_id       Post ID. Default is the ID of the `$post` global.
101  * @param string|array $size Optional. Registered image size to use, or flat array of height
102  *                           and width values. Default 'post-thumbnail'.
103  * @param string|array $attr Optional. Query string or array of attributes. Default empty.
104  * @return string
105  */
106 function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' ) {
107         $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
108         $post_thumbnail_id = get_post_thumbnail_id( $post_id );
109
110         /**
111          * Filter the post thumbnail size.
112          *
113          * @since 2.9.0
114          *
115          * @param string $size The post thumbnail size.
116          */
117         $size = apply_filters( 'post_thumbnail_size', $size );
118
119         if ( $post_thumbnail_id ) {
120
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 string $post_id           The post ID.
129                  * @param string $post_thumbnail_id The post thumbnail ID.
130                  * @param string $size              The post thumbnail size.
131                  */
132                 do_action( 'begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
133                 if ( in_the_loop() )
134                         update_post_thumbnail_cache();
135                 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
136
137                 /**
138                  * Fires after fetching the post thumbnail HTML.
139                  *
140                  * @since 2.9.0
141                  *
142                  * @param string $post_id           The post ID.
143                  * @param string $post_thumbnail_id The post thumbnail ID.
144                  * @param string $size              The post thumbnail size.
145                  */
146                 do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
147
148         } else {
149                 $html = '';
150         }
151         /**
152          * Filter the post thumbnail HTML.
153          *
154          * @since 2.9.0
155          *
156          * @param string $html              The post thumbnail HTML.
157          * @param string $post_id           The post ID.
158          * @param string $post_thumbnail_id The post thumbnail ID.
159          * @param string $size              The post thumbnail size.
160          * @param string $attr              Query string of attributes.
161          */
162         return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr );
163 }