Nov

02

WordPress Display All Images Associated with Page

Posted by CodeMunkyX

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: , ,

Socialize

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

featured wordpress themes