WordPress at a glance

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.000544s = slow | 50000 times = 0.36s = very fast | PHP 7.0.8, WP 4.6
Hooks in function
Return

String/false. False on failure. Attachment caption on success.

Usage

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

Examples

#1 Get the attachment caption

Suppose we have an attachment with ID 654 and caption "Today I caught such a big fish here!":

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

Code of wp get attachment caption: wp-includes/post.php WP 5.2.2

<?php
function wp_get_attachment_caption( $post_id = 0 ) {
	$post_id = (int) $post_id;
	if ( ! $post = get_post( $post_id ) ) {
		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 );
}

Related Functions

From tag: caption (of image)

More from category: Images

No comments
    Hello, !     Log In . Register