WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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.

share|improve this answer
thank you very much, I'll take a look as soon as possible! – vitto Feb 12 '12 at 14:55
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. – chrisguitarguy Aug 10 '12 at 16:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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