Get 1 attachment per post from X category outside the loop
-
Hey,
Hope someone can help with this. What I want is to display 5 attachments but only 1 attachment per post (so 1 attachment from a total of 5 different posts) 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.
<?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
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Get 1 attachment per post from X category outside the loop’ is closed to new replies.