Is it possible to get the second image from the gallery with the post_id?

I usually work with get_the_post_thumbnail but in this case I would like to get the second image from the post gallery, so a different image from the post's featured image.

Does exist some tutorial to get it?

  • When you say a specific image, what do you mean?
    – mor7ifer
    Feb 12 '12 at 14:26
  • I've fixed the question
    – vitto
    Feb 12 '12 at 14:32

You can get all the images associated with a post with the following:

$args = array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'post_parent'    => $post_parent_id // you'll need to set this somewhere else
$images = get_children( $args )

To go from that to the 2nd image, you should just need to point to $images[1], but I'm not sure how that'd work, you might needa mess with the sort order a little.

  • thank you very much, I'll take a look as soon as possible!
    – vitto
    Feb 12 '12 at 14:55
  • 1
    Gallery is sorted by menu_order assuming the end user has dragged the attachments around (and saved) -- otherwise, they'll be in the order in which they were uploaded. Maybe adding 'orderby' => 'menu_order' will help, @vittorio. Aug 10 '12 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.