Get URLs, Captions & Titles for Images Attached to Posts in Wordpress
I’ve been working on a new Wordpress theme for my client that involves a billboard concept where certain posts are categorized as homepage billboard items, and those post items have images attached to them within the Media gallery of Wordpress. I found out how to pull out the URI address of the images that are attached to the posts, but it took me some time to figure out how to get the Title, Caption and Description text associated directly with that image attachment.
I borrowed most of my code from here, but I think my example illustrates the concept of retrieving the data needed much easier.
// within the loop...
// gets last image from media library that are attached to current post
$attachments =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() );
if (empty($attachments)) {
$imageURI = "/location/to/default/image.jpg";
$imageTitle = "Default Title Text";
$imageCaption = "Default Caption Text";
$imageDescription = "Default Description Text";
}
else {
foreach($attachments as $attachment => $attachment_array );
$imagearray = wp_get_attachment_image_src($attachment, 'full', false);
$imageURI = $imagearray[0];
$imageID = get_post($attachment);
$imageTitle = $imageID->post_title;
$imageCaption = $imageID->post_excerpt;
$imageDescription = $imageID->post_content;
}
echo $imageURI;
echo $imageTitle;
echo $imageCaption;
echo $imageDescription;
Hope this solution works for your custom PHP queries and image retrieval via Wordpress' Media gallery.

Thanks for the link back. Glad you managed to find my code useful.
Whew! Thanks for this snippet, it was just what I needed to get captions in my theme based on the Arras theme.
How many pages can be added and pages will be indexed as if the reference to the new directory is not the main page, and a minor? What is the limit of the number of links from the main page? and how many pages are enough to complete the work site? Thanks for understanding!
———————————————————————
[url=http://www.scimmo.de]fun [/url][url=http://www.scimmo.de]fun games [/url][url=http://www.scimmo.de]flash games [/url][url=http://www.scimmo.de]shockwave games [/url]
Hey Scimmo, sorry for the late reply, but as I look at your comment post, I’m not sure what you are speaking about. Can you clarify?
how can i get all images related to one entry instead of just the first one?
Why not just use the [gallery] shortcode then?
Dude…you rock. Thank you for a concise solution that actually works.
Well…there is one error in your code; after your “foreach” statement, you have a semicolon. You should instead have an opening curly brace (and a corresponding closing brace).
Hey Mike, this is a great solution. I, too, would be interested in gathering the data for all images associated with a post because I want to pass the data to an XML file. The gallery tag doesn’t really work for that purpose. If you have any insight or ideas, I’d appreciate it. Thanks for the code!
Nevermind, figured out that piece from here!
http://codex.wordpress.org/Template_Tags/get_posts#Show_all_attachments
Glad you found your solution.
Can you give me some examples of why it might be useful to get this info as such? Examples?