Get all image attachments from a single WordPress post or page

This is probably one of my personal favorite snippets always very useful for sliders and gallerys, perfect for portfolio items. With this little snippet you’ll get all images attachments from a single WordPress post or page.

<?php
$args = array(
	'order'          => 'ASC',
	'post_type'      => 'attachment',
	'post_parent'    => $post->ID,
	'post_mime_type' => 'image',
	'post_status'    => null,
	'numberposts'    => -1,
);
$attachments = get_posts($args);
if ($attachments) {
	foreach ($attachments as $attachment) {
		echo wp_get_attachment_image($attachment->ID, 'custom-image-add-image-size-function');
	}
}
?>

Don’t forget to change ‘custom-image-add-image-size-function’ to your own specific image size.

Source Codex but, I think DIGWP do a better job of explaining this simple snippet.

About

A geek who likes to cook with wp ingredients!

Leave a Reply

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