DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Since WordPress 2.5, attachments management in WordPress have been improved and is now very powerful. Today, I’m going to show you a simple code snippet that you can use in your WordPress theme to display post attachments.

To achieve this recipe, just paste the following code anywhere in your post.php file and attachments will be displayed.

$args = array(
	'post_type' => 'attachment',
	'numberposts' => null,
	'post_status' => null,
	'post_parent' => $post->ID
$attachments = get_posts($args);
if ($attachments) {
	foreach ($attachments as $attachment) {
		echo apply_filters('the_title', $attachment->post_title);
		the_attachment_link($attachment->ID, false);

Credit : Snipplr.



  1. Great snippet! thanks
    is there a way to orgenize all the attachment in a list (ul)?
    and show it on a sidebar?
    I want to show each post’s specific attachments (in my case – PDF’s) on the sidebar.
    any suggestions?

  2. Very useful post!

    Gil, to organize them in a list, just wrap the entire block in <ul>
    </ul> tags (outside the php block), then wrap each iteration through the
    loop in <li></li> tags.

    $args = array(
    ‘post_type’ => ‘attachment’,
    ‘numberposts’ => null,
    ‘post_status’ => null,
    ‘post_parent’ => $post->ID
    $attachments = get_posts($args);

    if ($attachments) {
    foreach($attachments as $attachment){
    echo "<li>";
    echo apply_filters(‘the_title’, $attachment->post_title);
    the_attachment_link($attachment->ID, false);
    echo "</li>";

  3. This works on ly on posts but how do you get attachments of a certain custom post type? Anyone?

  4. Thanks Stephen
    I will definitely try it
    thanks for your help

  5. How would I combine this with a custom WP query? I want it to pull images from a page..
    the query earlier on..
    $bd_clients = new WP_Query(‘pagename=clients’);

    have_posts()) : $browndog_clients->the_post(); ?>

    Many thanks!

  6. @Dino, i also want the solution

  7. @Dino and @AliHussain

    I’m no expert, but I think the code should work because that it looks into the current post, but if you still need the code to do what you want you could mail me at neville (at) nevi (dot) me, and I’ll write a snippet for you. 🙂

  8. @Neville – I would really like to know how to grab the attached images from a specific post/page as well 🙂

  9. @Greg Use the PostID for ‘post_parent’ this should worke…

    $attachments = get_children( array(
    ‘post_parent’ => get_the_ID(),
    ‘numberposts’ => -1,
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘menu_order ASC’,
    ‘post_type’ => ‘attachment’,
    ‘post_mime_type’ => ‘image’) ); // image, video …

    foreach ( $attachments as $attachment_id => $attachment ) {
    echo wp_get_attachment_image($attachment_id);

  10. I think this is not correct, notice the following scenario:
    – create post with id 5
    – insert an image into that post
    — this image creates a post id 6 with parent_id 5
    So far so good
    Now if you you erase the image from post 5 and save, this code will still return the image as attachment, which is wrong.
    Other scenario, insert the same image into two diferente posts, post_parent can’t handle two ids.

    Isn’t this correct?

  11. This is great, it works on my project.. but i have problem when i added filtering of parent post.. it doesnt show the file attachement

    this is the code:

    function application_posts_filter( $query ) {

    global $pagenow, $post_id;

    if ( is_admin() && $pagenow == ‘edit.php’ && !empty($_GET[‘jobid’])) {

    $query->query_vars[‘post_parent’] = $_GET[‘jobid’];


    thanks for the help in advance 🙂

  12. This is great, Although, How can i make it so that it does not display the attachments title, and when the attachment is a image, it will display that image in fullsize?

    As of now it displays images as thumbnails.

  13. the best and simplist snippet of code i’ve found for attachments! thank you!

  14. Excellent snippet ! I’m totally PHP illiterate but fortunatly i can copy and paste 🙂
    I have this working now but i would love to see exactly this but in jquery fancybox. Is that possible ?

  15. how to show an image instead of the attachment link?

Leave a Comment

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