Make WordPress Core


Ignore:
Timestamp:
06/29/2016 05:28:00 PM (9 years ago)
Author:
joemcgill
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-thumbnail-template.php

    r37502 r37915  
    211211    }
    212212}
     213
     214/**
     215 * Returns the post thumbnail caption.
     216 *
     217 * @since 4.6.0
     218 *
     219 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
     220 * @return string Post thumbnail caption.
     221 */
     222function get_the_post_thumbnail_caption( $post = null ) {
     223    $post_thumbnail_id = get_post_thumbnail_id( $post );
     224    if ( ! $post_thumbnail_id ) {
     225        return '';
     226    }
     227
     228    $caption = wp_get_attachment_caption( $post_thumbnail_id );
     229
     230    if ( ! $caption ) {
     231        $caption = '';
     232    }
     233
     234    return $caption;
     235}
     236
     237/**
     238 * Displays the post thumbnail caption.
     239 *
     240 * @since 4.6.0
     241 *
     242 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
     243 */
     244function the_post_thumbnail_caption( $post = null ) {
     245    /**
     246     * Filters the displayed post thumbnail caption.
     247     *
     248     * @since 4.6.0
     249     *
     250     * @param string $caption Caption for the given attachment.
     251     */
     252    echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) );
     253}
Note: See TracChangeset for help on using the changeset viewer.