Post Pic

How to show WordPress post attachments

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.

17 Responses

Nov 07 2009 07:04

I don’t quite understand this? Where do you specify attachments? There isn’t anywhere when posting to specify attachments. Are you just talking about when you use the media upload?

Nov 07 2009 12:37

That’s interesting… didn’t know about attachments :)

Nov 08 2009 17:35

Attachments are simply any files you upload to a post using the media buttons

Nov 13 2009 11:29

Cool! this is all that I need. Many Thanks.

Nov 30 2009 09:58

i wish you could specific a category to show only attachments from that category…….

Nov 30 2009 14:33

you could, if i got time later on, i’ll post some updated code to do so …

Nov 30 2009 17:55

voila,

just ad ‘in_category’ around the function to filter category.

‘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);
}
}

}

?>

Nov 30 2009 17:56

‘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);
}
}

}

?>

Nov 30 2009 17:57

can’t pus code in the comments (or i just don’t know how).

so here, just add that if ( in_category( ‘about’ )) { before, and } after the function…

hope it helps

Jan 05 2010 23:11

Is it possible to extract just the first image attached to a post and display that from all my posts? That would leave me with a grid(CSS styled) of images on my blog that when clicked would open each post.

Jan 06 2010 01:25

if you want to show only the first atachement try this
[code]
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
$attachment = $attachments[0];
echo apply_filters('the_title', $attachment->post_title);
the_attachment_link($attachment->ID, false);

}

[/code]

i didn’t test it, but it should work

now if you want to list the fisrt attachement of each post on page, that’s something else ..

hope it helps

Jan 16 2010 23:12

Just an FYI that the thumbnail for this post has the iStockphoto watermark on it…

Thanks for the snippet.

Feb 01 2010 10:01

Thank you. This is a Good Article…

I pasted the code above in index.php, and it worked. But when I pasted it in attachment.php, It didn’t work.

Hope you can help me.
Thank you

Feb 03 2010 02:47

@justpri

the attachment.php page is meant to display a single attachment.

you could probably change it’s behavior, but i would recommend creating a special page to handle the task outputting all attachment of a post.

also note that this code snippet has to be inserted inside of the loop.

hope it helps

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required