Display the First Image Attachment of a WordPress Post


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



Copy this code and paste it in your HTML
  1. <?php
  2. $attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
  3. if ( ! is_array($attachments) ) continue;
  4. $count = count($attachments);
  5. $first_attachment = array_shift($attachments);
  6. ?>
  7. <?php echo wp_get_attachment_image($first_attachment->ID); ?>

URL: http://wordpress.org/support/topic/165921

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.