wp_get_attachment_caption

Retrieves the caption for an attachment.

Description

(string|false) wp_get_attachment_caption( (int) $post_id = 0 ); 

Returns (string|false)

False on failure. Attachment caption on success.

Parameters (1)

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

Usage

  1. if ( !function_exists( 'wp_get_attachment_caption' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. Attachment ID. Default is the ID of the global `$post`. 
  5. $post_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_get_attachment_caption($post_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function wp_get_attachment_caption( $post_id = 0 ) { 
  2. $post_id = (int) $post_id; 
  3. if ( ! $post = get_post( $post_id ) ) { 
  4. return false; 
  5.  
  6. if ( 'attachment' !== $post->post_type ) { 
  7. return false; 
  8.  
  9. $caption = $post->post_excerpt; 
  10.  
  11. /** 
  12. * Filters the attachment caption. 
  13. * @since 4.6.0 
  14. * @param string $caption Caption for the given attachment. 
  15. * @param int $post_id Attachment ID. 
  16. */ 
  17. return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );