Edit File by line
/home/barbar84/www/wp-inclu...
File: post-thumbnail-template.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WordPress Post Thumbnail Template Functions.
[2] Fix | Delete
*
[3] Fix | Delete
* Support for post thumbnails.
[4] Fix | Delete
* Theme's functions.php must call add_theme_support( 'post-thumbnails' ) to use these.
[5] Fix | Delete
*
[6] Fix | Delete
* @package WordPress
[7] Fix | Delete
* @subpackage Template
[8] Fix | Delete
*/
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Determines whether a post has an image attached.
[12] Fix | Delete
*
[13] Fix | Delete
* For more information on this and similar theme functions, check out
[14] Fix | Delete
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
[15] Fix | Delete
* Conditional Tags} article in the Theme Developer Handbook.
[16] Fix | Delete
*
[17] Fix | Delete
* @since 2.9.0
[18] Fix | Delete
* @since 4.4.0 `$post` can be a post ID or WP_Post object.
[19] Fix | Delete
*
[20] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
[21] Fix | Delete
* @return bool Whether the post has an image attached.
[22] Fix | Delete
*/
[23] Fix | Delete
function has_post_thumbnail( $post = null ) {
[24] Fix | Delete
$thumbnail_id = get_post_thumbnail_id( $post );
[25] Fix | Delete
$has_thumbnail = (bool) $thumbnail_id;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Filters whether a post has a post thumbnail.
[29] Fix | Delete
*
[30] Fix | Delete
* @since 5.1.0
[31] Fix | Delete
*
[32] Fix | Delete
* @param bool $has_thumbnail true if the post has a post thumbnail, otherwise false.
[33] Fix | Delete
* @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`.
[34] Fix | Delete
* @param int|false $thumbnail_id Post thumbnail ID or false if the post does not exist.
[35] Fix | Delete
*/
[36] Fix | Delete
return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Retrieve post thumbnail ID.
[41] Fix | Delete
*
[42] Fix | Delete
* @since 2.9.0
[43] Fix | Delete
* @since 4.4.0 `$post` can be a post ID or WP_Post object.
[44] Fix | Delete
* @since 5.5.0 The return value for a non-existing post
[45] Fix | Delete
* was changed to false instead of an empty string.
[46] Fix | Delete
*
[47] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
[48] Fix | Delete
* @return int|false Post thumbnail ID (which can be 0 if the thumbnail is not set),
[49] Fix | Delete
* or false if the post does not exist.
[50] Fix | Delete
*/
[51] Fix | Delete
function get_post_thumbnail_id( $post = null ) {
[52] Fix | Delete
$post = get_post( $post );
[53] Fix | Delete
[54] Fix | Delete
if ( ! $post ) {
[55] Fix | Delete
return false;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
return (int) get_post_meta( $post->ID, '_thumbnail_id', true );
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Display the post thumbnail.
[63] Fix | Delete
*
[64] Fix | Delete
* When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
[65] Fix | Delete
* is registered, which differs from the 'thumbnail' image size managed via the
[66] Fix | Delete
* Settings > Media screen.
[67] Fix | Delete
*
[68] Fix | Delete
* When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
[69] Fix | Delete
* size is used by default, though a different size can be specified instead as needed.
[70] Fix | Delete
*
[71] Fix | Delete
* @since 2.9.0
[72] Fix | Delete
*
[73] Fix | Delete
* @see get_the_post_thumbnail()
[74] Fix | Delete
*
[75] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
[76] Fix | Delete
* width and height values in pixels (in that order). Default 'post-thumbnail'.
[77] Fix | Delete
* @param string|array $attr Optional. Query string or array of attributes. Default empty.
[78] Fix | Delete
*/
[79] Fix | Delete
function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
[80] Fix | Delete
echo get_the_post_thumbnail( null, $size, $attr );
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Update cache for thumbnails in the current loop.
[85] Fix | Delete
*
[86] Fix | Delete
* @since 3.2.0
[87] Fix | Delete
*
[88] Fix | Delete
* @global WP_Query $wp_query WordPress Query object.
[89] Fix | Delete
*
[90] Fix | Delete
* @param WP_Query $wp_query Optional. A WP_Query instance. Defaults to the $wp_query global.
[91] Fix | Delete
*/
[92] Fix | Delete
function update_post_thumbnail_cache( $wp_query = null ) {
[93] Fix | Delete
if ( ! $wp_query ) {
[94] Fix | Delete
$wp_query = $GLOBALS['wp_query'];
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
if ( $wp_query->thumbnails_cached ) {
[98] Fix | Delete
return;
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
$thumb_ids = array();
[102] Fix | Delete
[103] Fix | Delete
foreach ( $wp_query->posts as $post ) {
[104] Fix | Delete
$id = get_post_thumbnail_id( $post->ID );
[105] Fix | Delete
if ( $id ) {
[106] Fix | Delete
$thumb_ids[] = $id;
[107] Fix | Delete
}
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
if ( ! empty( $thumb_ids ) ) {
[111] Fix | Delete
_prime_post_caches( $thumb_ids, false, true );
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
$wp_query->thumbnails_cached = true;
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
/**
[118] Fix | Delete
* Retrieve the post thumbnail.
[119] Fix | Delete
*
[120] Fix | Delete
* When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size
[121] Fix | Delete
* is registered, which differs from the 'thumbnail' image size managed via the
[122] Fix | Delete
* Settings > Media screen.
[123] Fix | Delete
*
[124] Fix | Delete
* When using the_post_thumbnail() or related functions, the 'post-thumbnail' image
[125] Fix | Delete
* size is used by default, though a different size can be specified instead as needed.
[126] Fix | Delete
*
[127] Fix | Delete
* @since 2.9.0
[128] Fix | Delete
* @since 4.4.0 `$post` can be a post ID or WP_Post object.
[129] Fix | Delete
*
[130] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
[131] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
[132] Fix | Delete
* width and height values in pixels (in that order). Default 'post-thumbnail'.
[133] Fix | Delete
* @param string|array $attr Optional. Query string or array of attributes. Default empty.
[134] Fix | Delete
* @return string The post thumbnail image tag.
[135] Fix | Delete
*/
[136] Fix | Delete
function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
[137] Fix | Delete
$post = get_post( $post );
[138] Fix | Delete
[139] Fix | Delete
if ( ! $post ) {
[140] Fix | Delete
return '';
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
$post_thumbnail_id = get_post_thumbnail_id( $post );
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Filters the post thumbnail size.
[147] Fix | Delete
*
[148] Fix | Delete
* @since 2.9.0
[149] Fix | Delete
* @since 4.9.0 Added the `$post_id` parameter.
[150] Fix | Delete
*
[151] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[152] Fix | Delete
* an array of width and height values in pixels (in that order).
[153] Fix | Delete
* @param int $post_id The post ID.
[154] Fix | Delete
*/
[155] Fix | Delete
$size = apply_filters( 'post_thumbnail_size', $size, $post->ID );
[156] Fix | Delete
[157] Fix | Delete
if ( $post_thumbnail_id ) {
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* Fires before fetching the post thumbnail HTML.
[161] Fix | Delete
*
[162] Fix | Delete
* Provides "just in time" filtering of all filters in wp_get_attachment_image().
[163] Fix | Delete
*
[164] Fix | Delete
* @since 2.9.0
[165] Fix | Delete
*
[166] Fix | Delete
* @param int $post_id The post ID.
[167] Fix | Delete
* @param int $post_thumbnail_id The post thumbnail ID.
[168] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[169] Fix | Delete
* an array of width and height values in pixels (in that order).
[170] Fix | Delete
*/
[171] Fix | Delete
do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
[172] Fix | Delete
[173] Fix | Delete
if ( in_the_loop() ) {
[174] Fix | Delete
update_post_thumbnail_cache();
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Fires after fetching the post thumbnail HTML.
[181] Fix | Delete
*
[182] Fix | Delete
* @since 2.9.0
[183] Fix | Delete
*
[184] Fix | Delete
* @param int $post_id The post ID.
[185] Fix | Delete
* @param int $post_thumbnail_id The post thumbnail ID.
[186] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[187] Fix | Delete
* an array of width and height values in pixels (in that order).
[188] Fix | Delete
*/
[189] Fix | Delete
do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
[190] Fix | Delete
[191] Fix | Delete
} else {
[192] Fix | Delete
$html = '';
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
/**
[196] Fix | Delete
* Filters the post thumbnail HTML.
[197] Fix | Delete
*
[198] Fix | Delete
* @since 2.9.0
[199] Fix | Delete
*
[200] Fix | Delete
* @param string $html The post thumbnail HTML.
[201] Fix | Delete
* @param int $post_id The post ID.
[202] Fix | Delete
* @param int $post_thumbnail_id The post thumbnail ID.
[203] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[204] Fix | Delete
* an array of width and height values in pixels (in that order).
[205] Fix | Delete
* @param string $attr Query string of attributes.
[206] Fix | Delete
*/
[207] Fix | Delete
return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Return the post thumbnail URL.
[212] Fix | Delete
*
[213] Fix | Delete
* @since 4.4.0
[214] Fix | Delete
*
[215] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
[216] Fix | Delete
* @param string|int[] $size Optional. Registered image size to retrieve the source for or a flat array
[217] Fix | Delete
* of height and width dimensions. Default 'post-thumbnail'.
[218] Fix | Delete
* @return string|false Post thumbnail URL or false if no image is available. If `$size` does not match
[219] Fix | Delete
* any registered image size, the original image URL will be returned.
[220] Fix | Delete
*/
[221] Fix | Delete
function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
[222] Fix | Delete
$post_thumbnail_id = get_post_thumbnail_id( $post );
[223] Fix | Delete
[224] Fix | Delete
if ( ! $post_thumbnail_id ) {
[225] Fix | Delete
return false;
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
return wp_get_attachment_image_url( $post_thumbnail_id, $size );
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Display the post thumbnail URL.
[233] Fix | Delete
*
[234] Fix | Delete
* @since 4.4.0
[235] Fix | Delete
*
[236] Fix | Delete
* @param string|int[] $size Optional. Image size to use. Accepts any valid image size,
[237] Fix | Delete
* or an array of width and height values in pixels (in that order).
[238] Fix | Delete
* Default 'post-thumbnail'.
[239] Fix | Delete
*/
[240] Fix | Delete
function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
[241] Fix | Delete
$url = get_the_post_thumbnail_url( null, $size );
[242] Fix | Delete
[243] Fix | Delete
if ( $url ) {
[244] Fix | Delete
echo esc_url( $url );
[245] Fix | Delete
}
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
/**
[249] Fix | Delete
* Returns the post thumbnail caption.
[250] Fix | Delete
*
[251] Fix | Delete
* @since 4.6.0
[252] Fix | Delete
*
[253] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
[254] Fix | Delete
* @return string Post thumbnail caption.
[255] Fix | Delete
*/
[256] Fix | Delete
function get_the_post_thumbnail_caption( $post = null ) {
[257] Fix | Delete
$post_thumbnail_id = get_post_thumbnail_id( $post );
[258] Fix | Delete
[259] Fix | Delete
if ( ! $post_thumbnail_id ) {
[260] Fix | Delete
return '';
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
$caption = wp_get_attachment_caption( $post_thumbnail_id );
[264] Fix | Delete
[265] Fix | Delete
if ( ! $caption ) {
[266] Fix | Delete
$caption = '';
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
return $caption;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
/**
[273] Fix | Delete
* Displays the post thumbnail caption.
[274] Fix | Delete
*
[275] Fix | Delete
* @since 4.6.0
[276] Fix | Delete
*
[277] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
[278] Fix | Delete
*/
[279] Fix | Delete
function the_post_thumbnail_caption( $post = null ) {
[280] Fix | Delete
/**
[281] Fix | Delete
* Filters the displayed post thumbnail caption.
[282] Fix | Delete
*
[283] Fix | Delete
* @since 4.6.0
[284] Fix | Delete
*
[285] Fix | Delete
* @param string $caption Caption for the given attachment.
[286] Fix | Delete
*/
[287] Fix | Delete
echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) );
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function