Skip to content

Instantly share code, notes, and snippets.

@thagxt
Created December 18, 2015 15:25
Show Gist options
  • Save thagxt/edd8d6341d0662c5ab34 to your computer and use it in GitHub Desktop.
Save thagxt/edd8d6341d0662c5ab34 to your computer and use it in GitHub Desktop.
Get All images attached to post & their image title, caption, description
<?php
/*
Getting all images attached to current post
+ getting the IMAGEs: alt, title, caption, description, url defined in Library for every image.
Place this script inside the loop.
*/
$args = array(
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image',
'numberposts' => -1,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) :
foreach ( $attachments as $attachment ) :
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $attachment->post_content;
$uri = wp_get_attachment_url( $attachment->ID);
?>
<h1><?php echo $title; ?></h1>
<a href="<?php echo $uri; ?>" rel="lightbox" title="<?php echo $img_title; ?>">
<img src="<?php echo $uri; ?>" alt="<?php echo $alt; ?>">
</a>
<p></p><?php echo $description; ?></p>
<?php endforeach; endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment