mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Post Thumbnails: Add helper functions for attachment captions.
This adds three new functions for getting/displaying attachment captions: * `wp_get_attachment_caption` - Retrieves a caption for a specific attachment. * `get_the_post_thumbnail_caption()` - Returns the post thumbnail caption. * `the_post_thumbnail_caption()` - Displays the post thumbnail caption. These are helpful for displaying a caption associated with an image directly in a template, rather than using the caption shortcode. This also introduces two new filters: * `wp_get_attachment_caption` - Filters the value of `wp_get_attachment_caption()`. * `the_post_thumbnail_caption` - Filters the display of the post thumbnail caption. `the_post_thumbnail_caption()` is automatically filtered by `wptexturize()`, `convert_smilies()`, and `convert_chars()` in `wp-includes/default-filters.php`. Props flixos90, joemcgill. Fixes #12235. git-svn-id: https://develop.svn.wordpress.org/trunk@37915 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -210,3 +210,44 @@ function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
|
||||
echo esc_url( $url );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the post thumbnail caption.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*
|
||||
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
|
||||
* @return string Post thumbnail caption.
|
||||
*/
|
||||
function get_the_post_thumbnail_caption( $post = null ) {
|
||||
$post_thumbnail_id = get_post_thumbnail_id( $post );
|
||||
if ( ! $post_thumbnail_id ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$caption = wp_get_attachment_caption( $post_thumbnail_id );
|
||||
|
||||
if ( ! $caption ) {
|
||||
$caption = '';
|
||||
}
|
||||
|
||||
return $caption;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the post thumbnail caption.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*
|
||||
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
|
||||
*/
|
||||
function the_post_thumbnail_caption( $post = null ) {
|
||||
/**
|
||||
* Filters the displayed post thumbnail caption.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*
|
||||
* @param string $caption Caption for the given attachment.
|
||||
*/
|
||||
echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user