Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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? –  m0r7if3r Feb 12 '12 at 14:26
    
I've fixed the question –  Vittorio Vittori Feb 12 '12 at 14:32
add comment

1 Answer 1

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! –  Vittorio Vittori 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. –  chrisguitarguy Aug 10 '12 at 16:53
add comment

Your Answer

 
discard

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.