Show All attached images for the current post excluding featured thumbnail

If you want to display all the attachments of current post excluding featured thumbnail then you can use following code snippet.

First we have to find featured image id. You can get featured image id if a thumbnail is set using get_post_thumbnail_id() function. This function returns id of the attached featured image.

<?php $post_thumbnail_id = get_post_thumbnail_id( $post_id );  ?>

You have to pass post id to get featured image id.

Complete code to display all attached images to current post excluding featured image

$args = array(
	'post_type'   => 'attachment',
	'numberposts' => -1,
	'post_status' => null,
	'post_parent' => $post->ID,
	'exclude'     => get_post_thumbnail_id()
	);

$attachments = get_posts( $args );

if ( $attachments ) {
	foreach ( $attachments as $attachment ) {
		echo apply_filters( 'the_title', $attachment->post_title );
		the_attachment_link( $attachment->ID, false );
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

One Comment

  1. Thanks, just used it 🙂