Get all image from single page using this query

I’m having some trouble with this sample widget code. I want to get all images (Minus the post thumbnail) from a page called “Gallery” but for some reason this is pulling all uploaded images from the entire site.

Also, how would I go about excluding the post thumbnail from this query?

  query_posts('pagename=gallery');
if (have_posts()) : 
echo "<ul class="recentwidget group photowidget">";
while (have_posts()) : the_post();
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => 1,
    'post_status' => null,
    'post_parent' => $post->ID
);

$attachments = get_posts( $args );
    if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
       echo '<li class="left imageshadow photolarge">';
       echo wp_get_attachment_image( $attachment->ID, 'full' );
       echo '</li>';
      }
    }
endwhile;

endif; 
wp_reset_query();

2 s
2

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