Get all image from single page using this query

Use get_children

I used this code to extract all the images from a page gallery in the chosen order. you can include this code in the loop or use it stand alone. just choose the appropriate post_parent code (see bellow the code example).

This example show all images associated to the page id 1, have a look:

        $images = get_children( array( 'post_parent' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) ); 
/* $images is now a object that contains all images (related to post id 1) and their information ordered like the gallery interface. */
        if ( $images ) { 

                //looping through the images
                foreach ( $images as $attachment_id => $attachment ) {
                ?>

                            <?php /* Outputs the image like this: <img src="" alt="" title="" width="" height="" /> */  ?> 
                            <?php echo wp_get_attachment_image( $attachment_id, 'full' ); ?>

                            This is the Caption:<br/>
                            <?php echo $attachment->post_excerpt; ?>

                            This is the Description:<br/>
                            <?php echo $attachment->post_content; ?>

                <?php
                }
        }

Find the post id you wan to extract images from and insert it into this argument:
'post_parent' => 1 (replace the 1 with your page id)

you can also use:

'post_parent' => $post->ID

If you want to use get_children in a loop, and get the post id from the returned post id.

If you want to exclude the image selected as a featured image i would have a if statement check if the image URL is equal to the featured image URL.

Hope this helps! 🙂

Leave a Comment