-1

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 } ?>
1

You can do this, and it isn't that hard though could be a lot of work for the server. The code, a bit simplified, should look something like this:

$cposts = wp_list_pluck($wp_query->posts,'ID');
// var_dump($cposts); // debug
$args = array(
  'post_type' => 'attachment',
  'post_mime_type' =>'image',
  'post_status' => 'inherit',
  'order'    => 'DESC',
  'posts__in' => $cposts
);
//   var_dump($args); // debug
$query_images = new WP_Query( $args );
// var_dump($query_images->posts); // debug

if ($query_images->have_posts()) {
  while ($query_images->have_posts()) {
    $query_images->the_post(); ?>
      <a href="<?php echo get_permalink($post->post_parent); ?>" title="<?php the_title_attribute(); ?>" ><?php echo wp_get_attachment_image(get_the_ID(), $size); ?></a><?php
  }
}

That code assumes that you are on a category archive and used the results already returned by the main query. This means that you will only get the images associated with the current page of paginated results.

0
<?php         global $post;
              $args = array( 
                'posts_per_page' => -1, 
                'offset'=> 1, 
                'category' => 9, 
                'orderby' => 'ID',
                'order' => 'ASC'
               );
              $myposts = get_posts( $args );
              foreach ( $myposts as $post ) : setup_postdata( $post ); 

           if (has_post_thumbnail( $post->ID ) ){
                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); 
                            $alt_text = get_post_meta( get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true );
                            $alt='';
                              if( !empty( $alt_text )){
                                   $alt = $alt_text;
                              } else {
                                  $alt = '';
                              }

                              $ImageSrc = $image[0]; 
             }
         ?>
<img src="<?php echo $ImageSrc; ?>" alt="<?php echo $alt; ?>" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.