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 there any possibility on Wordpress to get an image from a post gallery? Images are not inserted in the post, are on it's gallery. I'm trying to do a "galleries index" page and instead having the user set a "cover image" I'd like to take any of the attached to the post as part of a gallery.

I tried the wp_get_attachment_image() with no luck, I think because it works for inserted images, not the ones on it's gallery.

Thanks in advance.

Juan.-

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You can get the attached media to a post using get_children. IE: get the first attached image for post ID == 14

$args = array( 'post_mime_type' => 'image',
               'numberposts'    => 1,
               'post_parent'    => 14,
               'post_type'      => 'attachment' );

$first_attached_image = get_children( $args );
share|improve this answer
    
Hi MZAWeb, first let me thank you for your answer. I have tested this code, changed the ID to my post's ID, then tried to echo the $first_attached_image but as result I get the "array". One important thing, I'm querying custom post types (galleries) and want to show the list of all available galleries. I'm including this code within the query. Any ideas? –  Jaypee Jun 12 '12 at 16:10
    
get_children returns an associative array of posts (of variable type set by $output parameter) with post IDs as array keys, or false if no posts are found. So, try something like if $first_attached_image !== false then echo wp_get_attachment_image ( $first_attached_image[0]->ID ) –  MZAweb Jun 12 '12 at 16:16
    
Great! Thank you!! –  Jaypee Jun 12 '12 at 16:24

I hate suggesting plug-ins as an answer, but have you checked out Justin Tadlock's get the image plug-in? Worth dissecting for sure, great for learning, and it could definately help you with your problem.

http://wordpress.org/extend/plugins/get-the-image/

If you want to use native wordpress functions, just check out the codex. http://codex.wordpress.org/Function_Reference/wp_get_attachment_image

share|improve this answer

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.