How to list post attachments in WordPress

Here’s an example on how you can list documents uploaded to a specific post or page in WordPress using get_children.

Here’s an example on how you can list documents uploaded to a specific post or page in WordPress using get_children. Add the following code to your single.php or page.php file to list uploaded documents.

<?php
$attached_documents = get_children(array(
	'post_parent'    => get_the_ID(),
	'post_type'      => 'attachment',
	'numberposts'    => -1, // show all
	'post_status'    => null,
	'post_mime_type' => 'application/pdf,application/msword,application/vnd.ms-powerpoint,application/vnd.ms-excel'
));
if (!empty($attached_documents)):
	echo '<ul>';
		foreach($attached_documents as $attached_document) {
			$document_url   = wp_get_attachment_url($attached_document->ID);
			$document_title = apply_filters('the_title',$attached_document->post_title);

			echo '<li><a href="' . $document_url . '" title="' . $document_title . '" target="_blank">' . $document_title . '</a></li>';
		}
	echo '</ul>';
endif;
?>

This example lists all PDF, Word, PowerPoint and Excel files. For more MIME types, check out w3school’s comprehensive list.