Wordpress get attachments


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



Copy this code and paste it in your HTML
  1. /* Could be nicer. It adds in the <UL> code before and after checking if there are attachements,
  2. * but I only want to filter if attachments are bigger than (SIZE), so it may add an <UL> for
  3. * attachments that do not meet minimum requirements.
  4. */
  5. $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order') );
  6. if ($attachments > 0) {
  7. echo '<ul class="post-images">';
  8. }
  9. foreach ( $attachments as $attachment_id => $attachment ) {
  10. $attachment_meta = wp_get_attachment_metadata($attachment_id);
  11. if ($attachment_meta['width'] == 1280) {
  12. echo '<li>';
  13. echo wp_get_attachment_image($attachment->ID, 'full');
  14. echo '</li>';
  15. //echo $attachment_meta['width'];
  16. }
  17. }
  18. if ($attachments > 0) {
  19. echo '</ul>';
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.