wp_get_attachment_caption() WordPress Function

The wp_get_attachment_caption() function is used to get the caption for an attachment. This function can be used with any type of attachment, including images, audio files, and videos.

wp_get_attachment_caption( int $post_id ) #

Retrieves the caption for an attachment.


Parameters

$post_id

(int)(Optional) Attachment ID. Default is the ID of the global $post.


Top ↑

Return

(string|false) Attachment caption on success, false on failure.


Top ↑

Source

File: wp-includes/post.php

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 );
}


Top ↑

Changelog

Changelog
VersionDescription
4.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More