Created
March 4, 2012 06:56
-
-
Save BronsonQuick/1971067 to your computer and use it in GitHub Desktop.
A WordPress loop to display post/page attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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
Thanks