Hope someone can help with this. What I want is to display 5 attachments but only 1 attachment from each post from a specific category in the sidebar, which links to the posts permalink.
I’m using the following code so far which gets all attachments from all posts, but some posts have more than 1 attachment and I just want to show the first one, and link it to the permalink of the post.
So although the limit is 5 posts, if one post has 4 attachments then currently it will show 4 from one, and 1 from the other totalling 5, when what I want it to do is just show 1 from each of 5 different posts.
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => 5,
'post_status' => null,
'post_parent' => null, // any parent
'category_name' => 'work',
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_title();
the_permalink();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
?>
Cheers.
Dave
Cheers for that. I haven’t tested it yet but I did manage to get it working using the following code. I’m not a hardcore PHP programmer by any means, so I’m not sure which method is best. All I know right now is that what I have seems to work.
Thanks for having a crack at it!
Dave
Okay, I took a crack at this, I really don’t think there’s any elegant way without using a custom query;
The query gets the five most recent attachments whereby each attachment has a unique parent.
UPDATE:
Sorry, just realised you wanted to query by category too – add this big old join in after the SELECT;
And add this to the WHERE clause;