I think that episode is one of their best. The bit with Senna jumping out his car to help the other driver was inspiring.
Wordpress: Get Attachment Title and Description

Recently I’ve found myself with a problem in Wordpress. I’ve got a custom query going on which also uses a query to retrieve image attachments related to each post. I also needed to get additional information for the attachments. Strangely wp_get_attachment_metadata doesn’t return additional information such as caption or description, only the meta information like shutter speed and ISO settings.

Here’s how I managed to get around that problem.

$Images = & get_children ( ‘post_type=attachment&post_mime_type=image&post_parent=’ . $postID );

foreach ( $Images as $Image ) {
$NewImagesArray [] = $Image;
}

Keys = array ();

foreach ( $NewImages as $NewImage ) {
$Keys [] = $NewImage->ID;
$Captions [] = $NewImage->post_excerpt;
$Descriptions [] = $NewImage->post_content;
}

9 Comments to “Wordpress: Get Attachment Title and Description”

Mike says:

Thanks for posting this. Can you help me out a bit more? How do you echo this information to the page?

matt says:

thanks mate! i was just looking for a way to query my attachments and wasn’t sure how to do it. :)

jan says:

Hi ! Could you explain how would you echo the infos ? I’m struggling too to get it working. Thanks !

admin says:

Hi Jan,
If you look at the code snippet you’ll see that each piece of information for each image attachment is being pushed into an array…

You can get all the information from those arrays, alternatively, instead of pushing the info into arrays for use later, you can just echo out in that loop like so…

foreach ( $NewImages as $NewImage ) {
echo “image id:” + $NewImage->ID;
echo “image caption:” + $NewImage->post_excerpt;
echo “image description:” + $NewImage->post_content;
}

Hope this helps.

Mike says:

I developed a custom way to get the title caption and description of the image attachment in an example I illustrated on my site:

http://www.somethinginteractive.com/index.php/2009/07/23/get-images-captions-titles-for-images-attached-to-posts/

Cheers,
// mike

[...] borrowed most of my code from here, but I think my example illustrates the concept of retrieving the data needed much [...]

Karl says:

Thanks for posting! I was looking for a way to get the attachment caption in a query and managed to use $image->post_excerpt.

KJ says:

No worries Karl, glad you found it useful.

KJ

Les James says:

Just wanted to say thank you! This worked out perfectly for me.

Leave a Reply