According to WP codex the code copied below will return a list of images attached to a "page". I have yet see this happen and I'm at a complete loss as to why:

I have created a template page called gallery-test and within it I have pasted the code as in the example listed in the WP codex,

...I have set my page to the gallery-test template page. I have attached 5 images with titles to the page,

... the result is an empty page.

Any suggestions or help would be most appreciated!

    <?php if ( have_posts() ) : 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>';
               echo wp_get_attachment_image( $attachment->ID, 'full' );
               echo '<p>';
               echo apply_filters( 'the_title', $attachment->post_title );
               echo '</p></li>';

     endwhile; endif; ?>

1 Answer 1


For this feature to work the images must be uploaded to the media library from that page. In other words, clicking the "Add Media" button and adding images already in your media library won't do it. You must click "Add Media" then "Upload Images" and add them to the page.

  • Really? Deleting photos entirely, not leaving the in the library, and uploading entirely new photos is the only way to add/delete, or otherwise update, photos with this function?
    – ccbar
    Commented Apr 27, 2015 at 14:43
  • AFAIK you won't need to delete the photos from the library, just upload new copies to the post/page itself. Did you give that a try, if so what happened?
    – Brooke.
    Commented Apr 28, 2015 at 6:13
  • I'll give it a try and get back to you. But I will say this, when using get_children() oh boy! what a mess, the only way then to get an image not to show up in the list, even though you had removed it from the page, was to delete it from the library. and sometimes get_children didn't add the new image you had put in the list. Now I'm thinking it's because it was in the library and not newly uploaded to the page.
    – ccbar
    Commented Apr 28, 2015 at 13:00
  • finally getting back to this issue -- so using the code posted here, I inserted a new gallery w 4 images, images came in as a list just as they should. I deleted one of the images in the gallery - still had 4 images showing on front end. Then I deleted the gallery entirely, added a new gallery with 4 other images. Now I have a gallery list of 8 images (original 4 and new 4) even though I deleted old gallery and now have a new gallery w 4 more new images. Only by permanently deleting the images from the media library did I was I able to edit my gallery. Is the a tremendous BUG?!
    – ccbar
    Commented May 24, 2015 at 20:33

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.