Attachment List

Display Post Attachments in a List

| 1 Comment

While building an order tracking system, where I used Custom Meta Boxes to collect and store custom data about each order, I had a need to be able to upload documents (such as invoices and authorisations) to each order and display them on the front end for authorised users to view.

In order to do this, I built a custom page template for my custom post type and used the wp_get_attachment_link function in conjunction with a get_posts query to retrieve all of the attachments on the given post and display them in a list with a link to each document.

Within the loop, I used the following snippet:

The first half of the snippet runs a get_posts query to find all of the post attachments whose parent is the post that we’re currently viewing (attachments are their own post type and have a parent if they are associated with a particular post).

The second half of the snippet takes that array and for each one, it runs through the wp_get_attachment_link function to retrieve the link to the attachment, using the file title as the link text.

The end result is that you can drop a list of post attachments into an unordered list for viewing, like in this screenshot:

Attachment List

You can also modify the wp_get_attachment_link arguments to do an array of different things, like displaying an icon depending on the file type.

Categories: Code & Snippets | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Let a WordPress Expert help you

Do you want to try this, but feel like you need a helping hand, in case something goes wrong? My service, The WP Butler, gives you access to WordPress expertise whenever you need it. Better yet, I'll keep your site backed up, updated and secure, so that you don't have to worry about it. It's all part of the service. Use coupon DIWW and save 15% on all plans.

Visit The WP Butler


Dave has been tinkering with WordPress for many years, and he now shares his WordPress knowledge here on Do It WIth WordPress to help others realise its impressive power. He can also be hired to help with your WordPress needs. Dave, who is British, is married to his best friend, Marti, with whom he has a beautiful daughter, Ellie. When he's not dabbling with WordPress, he's probably eating Triscuits or hummus, watching an indie film or British TV show, spending time with friends or family, or exploring the world.

One Comment

  1. Nice snippet, I use this all the time for image sliders using similar arguments on get_posts and then using wp_get_attachment_url($post_attachments->ID).

Leave a Reply