WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get all images from a post with its captions and titles (4 posts)

  1. rodrego
    Member
    Posted 1 year ago #

    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):
    http://rodrigorego.com/clientes/sergio/proj1.html

    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!

  2. rodrego
    Member
    Posted 1 year ago #

    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?

  3. bcworkz
    Member
    Posted 1 year ago #

    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.

  4. rodrego
    Member
    Posted 1 year ago #

    Hi bcworkz,

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

    <?php
            $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 );
                      }
            }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic