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 ww.wp.xz.cn 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.
The page I need help with: [log in to see the link]
-
This topic was modified 8 years, 7 months ago by
-
When on
attachment.phpthe attachment (the media item) and the actual attachment page are the same. So the$attachment_idcan be found withget_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.phpI 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.
@davidsword, it works perfectly.
Thank you very much.
-
This reply was modified 8 years, 7 months ago by
The topic ‘Using the post ID’ is closed to new replies.