wp_get_attachment_caption()WP 4.6.0

Retrieves the caption of specified attachment.

Caption and title are different things. Do not confuse them:

1 time — 0.000544 sec (slow) | 50000 times — 0.36 sec (very fast) | PHP 7.0.8, WP 4.6
Hooks from the function

Return

String|false. Attachment caption on success, false on failure.

Usage

wp_get_attachment_caption( $post_id );
$post_id(int/WP_Post)
ID or WP_Post object of the attachment.
Default: current post

Examples

0

#1 Get the caption of the attachment image

Let's say we have an attachment with ID 654 and the caption "Today I caught such a big fish here!" then:

echo wp_get_attachment_caption( 654 ); //> output: Today I caught such a big fish here!

Changelog

Since 4.6.0 Introduced.

wp_get_attachment_caption() code WP 6.4.3

function wp_get_attachment_caption( $post_id = 0 ) {
	$post_id = (int) $post_id;
	$post    = get_post( $post_id );

	if ( ! $post ) {
		return false;
	}

	if ( 'attachment' !== $post->post_type ) {
		return false;
	}

	$caption = $post->post_excerpt;

	/**
	 * Filters the attachment caption.
	 *
	 * @since 4.6.0
	 *
	 * @param string $caption Caption for the given attachment.
	 * @param int    $post_id Attachment ID.
	 */
	return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
}