WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/29/2016 05:28:00 PM (6 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.php

    r37890 r37915  
    49384938
    49394939/**
     4940 * Retrieves the caption for an attachment.
     4941 *
     4942 * @since 4.6.0
     4943 *
     4944 * @param int $post_id Optional. Attachment ID. Default 0.
     4945 * @return string|false False on failure. Attachment caption on success.
     4946 */
     4947function wp_get_attachment_caption( $post_id = 0 ) {
     4948    $post_id = (int) $post_id;
     4949    if ( ! $post = get_post( $post_id ) ) {
     4950        return false;
     4951    }
     4952
     4953    if ( 'attachment' !== $post->post_type ) {
     4954        return false;
     4955    }
     4956
     4957    $caption = $post->post_excerpt;
     4958
     4959    /**
     4960     * Filters the attachment caption.
     4961     *
     4962     * @since 4.6.0
     4963     *
     4964     * @param string $caption Caption for the given attachment.
     4965     * @param int    $post_id Attachment ID.
     4966     */
     4967    return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
     4968}
     4969
     4970/**
    49404971 * Retrieve thumbnail for an attachment.
    49414972 *
Note: See TracChangeset for help on using the changeset viewer.