Title: Using the post ID
Last modified: November 3, 2017

---

# Using the post ID

 *  Resolved [justwander](https://wordpress.org/support/users/justwander/)
 * (@justwander)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/using-the-post-id/)
 * Hello,
    I am looking to add the image caption text to the attachment page on 
   my site. At present I get only the image description.
 * I found the following function in a post on the WordPress.org site:
 *     ```
       function wp_get_attachment( $attachment_id ) {
   
       	$attachment = get_post( $attachment_id );
       	return array(
       		'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
       		'caption' => $attachment->post_excerpt,
       		'description' => $attachment->post_content,
       		'href' => get_permalink( $attachment->ID ),
       		'src' => $attachment->guid,
       		'title' => $attachment->post_title
       	);
       }
       ```
   
 * Call the function with:
    `$attachment_meta = wp_get_attachment(your_attachment_id);`
 * Then put the text on the webpage with:
    `echo $attachment_meta['caption'];`
 * Beginner that I am I understand most of what is here except this, how/where do
   I get the ID number?
 * When I look at the function call I assume that the “attachment” is not the page
   I am building but the image I am putting on the page. Since the image appears
   on the page OK the ID was used, right? Where do I get it to include in the $attachment_meta
   call?
 * ps
    If you go to the URL included with this question just click on an image to
   go to the attachment page for the image.
    -  This topic was modified 8 years, 7 months ago by [justwander](https://wordpress.org/support/users/justwander/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fusing-the-post-id%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [David Sword](https://wordpress.org/support/users/davidsword/)
 * (@davidsword)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/using-the-post-id/#post-9649471)
 * When on `attachment.php` the attachment (the media item) and the actual attachment
   page are **the same**. So the `$attachment_id` can be found with `get_the_ID()`.
 * –
 * I put the following in my themes and it gave me the caption as well as description.
 * declared the custom function in `functions.php` (as it’s not part of WordPress,
   just someones idea!)
 *     ```
       function wp_get_attachment( $attachment_id ) {
         $attachment = get_post( $attachment_id );
         return array(
           'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
           'caption' => $attachment->post_excerpt,
           'description' => $attachment->post_content,
           'href' => get_permalink( $attachment->ID ),
           'src' => $attachment->guid,
           'title' => $attachment->post_title
         );
       }
       ```
   
 * in `attachment.php` I then call for the extra data within the loop, and show 
   what it contains for confirmation it works
 *     ```
       $attachment_meta = wp_get_attachment(get_the_ID());
   
       echo "<pre>";
       print_r(￼$attachment_meta);
       echo "</pre>";
       ```
   
 * and more specifically, gather what’s needed
 *     ```
       echo "Caption: {$attachment_meta['caption']}<br />";
       echo "Description: {$attachment_meta['description']}<br />";
       ```
   
    -  This reply was modified 8 years, 7 months ago by [David Sword](https://wordpress.org/support/users/davidsword/).
 *  Thread Starter [justwander](https://wordpress.org/support/users/justwander/)
 * (@justwander)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/using-the-post-id/#post-9649536)
 * [@davidsword](https://wordpress.org/support/users/davidsword/), it works perfectly.
 * Thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Using the post ID’ is closed to new replies.

## Tags

 * [$post->id](https://wordpress.org/support/topic-tag/post-id/)
 * [attachment page](https://wordpress.org/support/topic-tag/attachment-page/)
 * [caption](https://wordpress.org/support/topic-tag/caption/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [justwander](https://wordpress.org/support/users/justwander/)
 * Last activity: [8 years, 7 months ago](https://wordpress.org/support/topic/using-the-post-id/#post-9649536)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
