X

Output All Attached Images as a Gallery

This is a fun snippet that'll output all of your image attachments into an unordered list of thumbnails linking to the full-sized images. You can use this to create a gallery of your attachments based on the post ID. To use this snippet, add it to the loop of your single theme template.

	$args = array(
		'post_type' => 'attachment',
		'post_mime_type' => 'image',
		'numberposts' => -1,
		'post_status' => null,
		'post_parent' => $post->ID
	);
	$attachments = get_posts( $args );
	if ( $attachments ) {
		echo '<ul id="gallery" class="thumbs">';
			foreach ( $attachments as $attachment ) {
			$large_image = wp_get_attachment_image_src($attachment->ID,'large',false);
			echo '<li><a href="' . $large_image&#91;0&#93; . '">'. wp_get_attachment_image( $attachment->ID, 'thumbnail' ) .'</a></li>';
		}
		echo '</ul>';
	}

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!