Get all first images of posts in same category

I am trying to build a list of thumbnail images linked to their associated single content pages within the same category. So, when I am in the category of ‘x’ the list returns all image attachments from every post in category ‘x’.

I have the following code in my sidebar which is brought into only the content-single pages. However this just returns the current posts image and not other posts in the same category.

What am I doing wrong? EDIT: Or is this even possible?

<?php if ( is_single()) {
<h3><?php $category = get_the_category(); echo $category[0]->cat_name; ?></h3>
<ul class="thumbnail-navigation">
<?php while ( have_posts() ) : the_post(); ?>
$size = 'thumbnail';
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'numberposts' => -1));
foreach ( $attachments as $thumb_id => $attachment )
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php echo wp_get_attachment_image($thumb_id, $size); ?></a>

<?php endwhile;  ?>
<?php }; ?>

Read more here: Get all first images of posts in same category

Leave a Reply

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