bgallagh3r
8/5/2012 - 3:05 AM

WordPress: Fetch image attachments from child pages.

WordPress: Fetch image attachments from child pages.

<?php 
global $post;
// Get All children of current page.
$children = get_pages(array('child_of' => $post->ID));
// Increment value.
$i = 0;
foreach ($children as $child){
    // Get Image Source
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $child->ID ), 'thumbnail' ); 
    // Get Child Title to use as ALT 
    $title = get_the_title($child->ID);
    // Check if image is even or odd class.
    $class = $i % 2 == 0 ? "even" : "odd";
    // Echo image.
    echo "<img src=\"$image\" alt=\"$title\" class=\"$class\">";
    $i++;
}
unset($i, $image, $title, $class);
 ?>