WordPress : Function to get all images attached to a post/page

November 15, 2015

When you upload a set of images to a post/page using the ‘Add Media’ button when adding or editing a post they are attached to that post (as well as being available for use throughout the site) should you wish to insert a gallery.

You can see which images are attached to that post by opening up the media window, going to the ‘Media Library’ tab and changing the dropdown to ‘Uploaded to this post’.

You can then use WordPress’ built in image gallery to embed all those images in a gallery should you wish, using the ‘Create Gallery’ link on the left of the media popup.

However if you want to have a bit of control over the layout of that gallery you can opt to pull out all image attached to that post and display them using the HTML you wish around it.

Below is a function you can add to functions.php and then call in your template.

Once that is added to functions.php you can simply add the following PHP to your template to display all the images, giving the image size you wish to use (e.g. large, medium, thumbnail, full etc).