Nov
02
WordPress Display All Images Associated with Page
This is a code snippet I use quite often to display custom galleries in my themes. Paste the following code within the loop of your page template.
echo '<ul id="Thumbnails">';
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID ); // returns an array
$imgTitle = $attachment->post_title;
$imgCaption = $attachment->post_excerpt;
$imgDescription = $attachment->post_content;
echo '<li><a href="' . $attachment->guid . '"><img ';
if ( $attachment->post_name == $_GET['image'] ) { echo 'class="active"'; }
echo ' src="' . $attachment->guid . '" title="' . $imgCaption . '" alt="' . $imgTitle . ' - ' . $imgDescription . '" /></a></li>' . "\n";
}
}
echo '</ul>' . "\n";
echo '<div style="clear: both;"></div>';
Tags: gallery, image, WordPress


