WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code (see below) that lists all attached images from a post. I'm trying to alter this code to only list the first five images, and also to exclude the featured image from the list. Any help is certainly appreciated. Thanks in advance.

<div class="entry-content">

     $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, 'thumbnail' );
               echo '</li>';
</div><!-- .entry-content -->
share|improve this question
up vote 1 down vote accepted

Did you try this:

 $args = array(
   'post_type'      => 'attachment',
   'posts_per_page' => 5,
   'post_status'    => 'inherit',
   'post_parent'    => $post->ID,
   'exclude'        => get_post_thumbnail_id( $post->ID )
share|improve this answer
Thank you @birgire! This worked perfectly. Much appreciated. – user1512680 Nov 22 '13 at 19:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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