Skip to content

Instantly share code, notes, and snippets.

@bgallagh3r
Created August 5, 2012 03:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bgallagh3r/3261304 to your computer and use it in GitHub Desktop.
Save bgallagh3r/3261304 to your computer and use it in GitHub Desktop.
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);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment