This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

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 code and be nice ^_^

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.