Support » Plugins » Hacks » Get all images from a post with its captions and titles

  • Resolved rodrego


    Hi folks,

    I’m trying to get all the images from a post with their titles and captions, so I can display them in a slideshow structure (which I already have).

    Here’s what I’m trying to do (only demo html by now):

    All the images in the slideshow belong to the same post. The title of the painting should be the image title and the text below it should be the image caption. So when I click the arrow, another image, with its title and caption appears.

    What’s the best way to do it?

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Almost forgot: each image goes in a different div, so I can’t have a code that just gets me a list of images, I suppose.

    Instead, I think I need a code that allows me to do something like “the first attached image of this post goes on first div, 2nd one goes on this other div, 3rd goes on third div, and if there are no more images, no more divs are created”.

    Is this feasible?

    Moderator bcworkz


    You can query attached images the same way you would query posts. Except the post_type is ‘attachment’ and the post_parent is the post ID of the post to which the images are attached.

    The query will return the attachment objects in an array. You can loop through the array just like any other posts. In the loop, you echo out the div tags and whatever else HTML you need. The image path is stored in the guid field, the title in post_title and the caption in post_excerpt.

    Hi bcworkz,

    I found a solution more or less like that. Here it goes:

            $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' =>'any', 'post_parent' => $post->ID );
             $attachments = get_posts($args);
             if ($attachments) {
                     foreach ( $attachments as $attachment ) {
    			echo apply_filters( 'the_title' , $attachment->post_title );
    			echo apply_filters( 'the_excerpt' , $attachment->post_excerpt );
    			the_attachment_link( $attachment->ID , true );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get all images from a post with its captions and titles’ is closed to new replies.