Skip to content

Instantly share code, notes, and snippets.

@soggybag
Created November 28, 2015 05:10
Show Gist options
  • Save soggybag/e7d815d01b49e7d1a61c to your computer and use it in GitHub Desktop.
Save soggybag/e7d815d01b49e7d1a61c to your computer and use it in GitHub Desktop.
WordPress, simple function fetches all images uploaded to the current post.
/*
This function fetches all of the images uloaded to the current post.
Pass in the image size, and the number of images to return.
Use this function in the loop.
*/
function get_images_in_current_post( $size="thumbnail", $num=-1 ) {
global $post;
$attachments = get_posts( array("numberposts" => $num,
"post_type" => "attachment",
"post_parent" => $post->ID,
"orderby" => "name" ) );
// Loop through array
// [img, img, img ... ]
foreach( $attachments as $attachment ) {
$img = wp_get_attachment_image( $attachment->ID, $size );
echo "<div class='post-img'>$img</div>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment