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();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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! 🙂

Method 2

Easiest way is to use get_attached_media(). There are many different ways, almost all of them covered here


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x