0

I'm creating an archive page for my photoblog.

My posts have 1 or more images. I'd like to show all the attachment thumbnails in a page. Using the featured image (the_post_thumbnail) only shows 1 thumbnail, I'd like to show all the attached images thumbnails.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php 
  $thumbnails = get_posts('numberposts=30');
  foreach ($thumbnails as $thumbnail) {
    if ( has_post_thumbnail($thumbnail->ID)) {
      echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
      echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
      echo '</a>';
    }
  }
?>

<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>

The code above only shows the featured image, if a post doesn't have a featured image, it's not shown.

<?php
$args = array(
    'post_type' => 'attachment', 
    'posts_per_page' => -1, 
    'order'=> 'DESC', 
    'orderby' => 'date',
    ); 
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $post ) {
        setup_postdata( $post );
        the_attachment_link( $post->ID, false );
    }
    wp_reset_postdata();
}
?>

The above code shows all attachment thumbnails. How do I:-

  1. Have it sorted by post date (it's currently being sorted by attachment upload date),
  2. Thumbnail linked to post it is attached to?

Or is there another way to do what I want?

Any help is appreciated.

2

See the below code.

<?php
$args = array( 'posts_per_page' => -1, 'order'=> 'DESC', 'orderby' => 'date' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
  setup_postdata( $post ); ?> 
    <div>
    <?php
    $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID ); 
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
    //  echo apply_filters( 'the_title' , $attachment->post_title );
        the_attachment_link( $attachment->ID , false );
    }
}
    ?>
    </div>
<?php
endforeach; 
wp_reset_postdata();
?>
  • Thank you for the reply! I changed the_attachment_link($attachment->ID , false) to wp_get_attachment_image($attachment->ID) and wrapped it with a link to the post permalink. :) – ayutheos Oct 18 '13 at 1:04

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.