0

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">
<ul>
    <?php 

     $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>';
              }
         }
    ?>
</ul>
</div><!-- .entry-content -->
1

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 )
  );
  • Thank you @birgire! This worked perfectly. Much appreciated. – bprocell Nov 22 '13 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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