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:

<?php // get post attachments
$post_attachments = get_posts( array (
	'post_type' => 'attachment',
	'post_parent' => $post->ID
));
?>
<ul>
	<?php foreach ( $post_attachments as $post_attachment ) {
		echo '<li>' . wp_get_attachment_link( $post_attachment->ID, '', false, false ) . '</li>';
	} ?>
</ul>

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.

Keep your site backed up, updated & secure

I provide a service called The WP Butler, which helps you stay on top of the maintenance of your WordPress site. Instead of worrying about whether your site is secure, updated and backed up, The WP Butler handles all that for you on a regular basis, so that you can focus on doing what you do best. If you use coupon DIWW, you'll save 15% on our already-low-prices for all maintenance plans.

Author: Dave Clements

Dave Clements has been building websites for close to a decade and in 2010, he formalised that by starting his own company, The UK Edge. He now works on a variety of web projects, from simple tasks like installing a new WordPress site, to consulting on problems, or redesigning his clients' sites. He also runs Do It With WordPress, a site dedicated to providing free tutorials on WordPress. When he's not building your new website, you can find Dave eating Wheat Thins, spending time with friends and family, watching Indie films, fostering kittens from the local Humane Society, listening to some dubstep, dance and electronic rock, and 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