]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/post-thumbnail-template.php
Wordpress 3.3
[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  * Themes function.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 $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 /**
38  * Display Post Thumbnail.
39  *
40  * @since 2.9.0
41  *
42  * @param int $size Optional. Image size.  Defaults to 'post-thumbnail', which theme sets using set_post_thumbnail_size( $width, $height, $crop_flag );.
43  * @param string|array $attr Optional. Query string or array of attributes.
44  */
45 function the_post_thumbnail( $size = 'post-thumbnail', $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 function update_post_thumbnail_cache() {
55         global $wp_query;
56
57         if ( $wp_query->thumbnails_cached )
58                 return;
59
60         $thumb_ids = array();
61         foreach ( $wp_query->posts as $post ) {
62                 if ( $id = get_post_thumbnail_id( $post->ID ) )
63                         $thumb_ids[] = $id;
64         }
65
66         if ( ! empty ( $thumb_ids ) ) {
67                 get_posts( array(
68                                 'update_post_term_cache' => false,
69                                 'include' => $thumb_ids,
70                                 'post_type' => 'attachment',
71                                 'post_status' => 'inherit',
72                                 'nopaging' => true
73                 ) );
74         }
75
76         $wp_query->thumbnails_cached = true;
77 }
78
79 /**
80  * Retrieve Post Thumbnail.
81  *
82  * @since 2.9.0
83  *
84  * @param int $post_id Optional. Post ID.
85  * @param string $size Optional. Image size.  Defaults to 'thumbnail'.
86  * @param string|array $attr Optional. Query string or array of attributes.
87  */
88 function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' ) {
89         $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
90         $post_thumbnail_id = get_post_thumbnail_id( $post_id );
91         $size = apply_filters( 'post_thumbnail_size', $size );
92         if ( $post_thumbnail_id ) {
93                 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
94                 if ( in_the_loop() )
95                         update_post_thumbnail_cache();
96                 $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
97                 do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
98         } else {
99                 $html = '';
100         }
101         return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr );
102 }
103
104 ?>