Last Updated: February 25, 2016
·
639
· briancaicco

Display Post Attachments

<?php 
function dt_get_images($post_id) {
global $post;
//Define Qurey
$images = get_children( 
    array(
        'post_parent' => $post_id, 
        'post_status' => 'inherit', 
        'post_type' => 'attachment', 
        'post_mime_type' => 'image', 
        'order' => 'ASC', 
        'orderby' => 'menu_order ID'
        )
    );
foreach ($images as $attachment_id => $image) {
    //Define image meta
    $img_title = $image->post_title;   // title.
    $img_caption = $image->post_excerpt; // caption.
    $img_description = $image->post_content; // description.
    //Build image meta
    $img_alt = get_post_meta($attachment_id,'_wp_attachment_image_alt', true); //alt
    if ($img_alt == '') : $img_alt = $img_title; endif;
    //Build image url
    $full_array = image_downsize( $image->ID, 'full' );
    $img_url_full = $full_array[0];
    //Display Image
    echo'<img src="'.$img_url_full.'" alt="'.$img_alt.'" title="'.$img_title.'" />';
    }
}
?>
//Call within the loop
<?php dt_get_images($post->ID); ?>