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?

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>';

  • Apparently the use of query_posts is not without drawbacks. Have you tried using get_posts instead? But then, I think using The Loop at all in this case is overkill. Just hardcode the page ID into your call to get_posts (which is currently inside The Loop).
    – Raphael
    Jan 17, 2012 at 20:14
  • Unfortunately my client is a fan of query_posts (Something to do with them being able to edit it later on should they need to). Is there no way to do it using query_posts ? Jan 17, 2012 at 23:33
  • No, sorry. I have not really done something like this myself, and from skimming the Codex your code looks reasonable to me (besides coding style cough). Oh, it is contract work. In that case you probably want to wrap your feature into a smallish plugin with an options page so they can't break it (and you can reuse it). Maybe even use an existing plugin?
    – Raphael
    Jan 18, 2012 at 9:58
  • also, i would recommend you to use WP_query if you are creating secondary loops for example in widgets. but if you must use query_posts function to alter the main loop, remember to add pagination to the query_posts argument list. scribu.net/wordpress/wp-pagenavi/… Jan 19, 2012 at 22:31

2 Answers 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; ?>


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! :)

  • This worked perfectly. Although, is there a way to change the output of the image? For example, adding a specific class to them? Jan 20, 2012 at 10:43
  • Nice to be helpfull :) if you add this array into the list of arguments for the function wp_get_attachment_image like this: wp_get_attachment_image($attachment_id, 'full', array('class' => "your-calss" )); I believe you get the desired effekt, havent tried though. Jan 20, 2012 at 17:51
  • 1
    codex.wordpress.org/Function_Reference/wp_get_attachment_image <-- see this codex page for more info :) Jan 20, 2012 at 17:55
  • 1
    Thanks for that. In the end I just wrapped the image output into a list and I got the desired effect. Jan 20, 2012 at 18:45
  • It worked for me when I replaced 'post_parent' => 1 with 'post_parent' => get_the_ID()
    – Francis
    Apr 3, 2023 at 15:27

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

  • Please edit your answer and at least add a brief of all the ways and then share the link. A link-only answer will become irrelevant when the link comes dead. Nov 23, 2014 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.