WordPress.org

Ready to get started?Download WordPress

Forums

Getting thumbnails for post attachments (1 post)

  1. sublink
    Member
    Posted 1 year ago #

    Hi there,

    I'm looking to show thumbnails for all the image attachments that belong to a post.

    $thumbs = get_posts(array(
      'post_type' => 'attachment',
      'post_parent' => $post->ID,
      'post_mime_type'  => 'image',
    ));
    
    <?php foreach ($thumbs as $thumb): ?>
      <img src="<?php echo wp_get_attachment_thumb_url($thumb->ID) ?>">
    <?php endforeach; ?>

    This is great, but now I'd like to be able to use custom thumbnail sizes, ideally like the ones I've set using the add_image_size function.

    <?php foreach ($thumbs as $thumb): ?>
      <?php echo wp_get_attachment_image($thumb->ID, 'custom-size') ?>
    <?php endforeach; ?>

    This has two problems though. Firstly, it doesn't respect the hard crop mode of custom sizes set with add_image_size. And secondly, it doesn't actually link to a resized image--it links to the original, full-size image and just sets the image width and height attributes to the thumbnail size.

    I've also tried using the get_the_post_thumbnail function, passing in the attachment id, but that doesn't work. I'm guessing it looks for the a child post of the passed-in id.

    So, any ideas?

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic