Skip to content

Instantly share code, notes, and snippets.

@BronsonQuick
Created March 4, 2012 06:56
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save BronsonQuick/1971067 to your computer and use it in GitHub Desktop.
Save BronsonQuick/1971067 to your computer and use it in GitHub Desktop.
A WordPress loop to display post/page attachments
<?php /*
* This is just a simple loop to display 7 attachments attached to a post or page in WordPress
* You can change 'medium' and 'thumbnail' to add sizes you have definied in your theme by the add_image_size function
* in WordPress
*/
?>
<div id="thumbs" class="pic_list">
<ul class="thumbs">
<?php /* Time to create a new loop that pulls out the first 7 image attachments for this post */
$args = array( 'post_type' => 'attachment', 'numberposts' => 7, '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, 'medium' );
echo '<li><a class="thumb" href="';
echo $image_attributes[0];
echo '">';
echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
echo '</a></li>'; ?>
<?php } ?>
</ul>
<?php } ?>
</div>
@Missketer
Copy link

What if I would like the images to link back to the post how do I go about this

@miryamfv
Copy link

Thanks

@SolespireMarcus
Copy link

What if I would like the images to link back to the post how do I go about this

get_permalink($post->post_parent);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment