Wordpress insert post attachments at custom size linked to custom size


/ Published in: PHP
Save to your folder(s)

To insert custom loop of post attachments as images (at core or custom size) linked not to full size image but custom-configured (or core) size.


Copy this code and paste it in your HTML
  1. <?php
  2. $postID = $post->ID;
  3. //print_r($product_meta);
  4.  
  5. // (thumbnail, medium, large or full) or a 2-item array
  6. // representing width and height in pixels, e.g. array(32,32)
  7. // OR the custom config'd name of image size
  8. $image_size = 'prods-large'; // the image size to be shown initially (smaller version)
  9. $image_size_lg = 'large'; // the image size to link to (large version)
  10.  
  11. $images = get_children(array(
  12. 'post_parent' => $postID,
  13. 'post_type' => 'attachment',
  14. 'numberposts' => -1,
  15. 'order' => 'ASC',
  16. 'orderby' => 'menu_order',
  17. //'exclude' => get_post_thumbnail_id(), -- uncomment to exclude post thumbnail
  18. 'post_mime_type' => 'image',
  19. )
  20. );
  21.  
  22. //print_r($images);
  23. if($images) {
  24.  
  25. echo '<div class="prod-imgs">';
  26.  
  27. foreach( $images as $image ) {
  28.  
  29. $attachment_id = $image->ID;
  30.  
  31. // get the 'large' size image's URL
  32. $image_attributes = wp_get_attachment_image_src( $attachment_id, 'large' ); // returns an array - [0] is the URL of the image 'large' size
  33. $largeSizeURL = $image_attributes[0];
  34.  
  35. echo '<a class="attachment-' . $attachment_id . ' popup-with-zoom-anim img-link" href="' . $largeSizeURL . '">';
  36. echo wp_get_attachment_image($attachment_id, $image_size);
  37. echo '</a>';
  38.  
  39. }
  40. echo '</div>';
  41. }
  42. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.