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(); ?>
<li>
<?php
$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>

</li>
<?php endwhile;  ?>
</ul>
<?php }; ?>

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


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: