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.


2 Answers 2


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 );
  • 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
    Commented Jun 12, 2012 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
    Commented Jun 12, 2012 at 16:16

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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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