Wordpress - How to get gallery images?

I had the same problem - how can I display all the images from a post containing a gallery where some of the images are not attached to the post, or are attached to different posts?

The new media interface in WP3.5 allows you to add any of the images in the media library to a gallery, regardless of whether they are "attached" or not. As you've discovered, the "get_children" function only returns images that are attached to the post. The trick I used is to get the ids from the [gallery] shortcode itself rather than from the fact that they are attachments. The shortcode includes all of the image ids, regardless of whether they are attached to the post or not. E.g. [gallery ids="410,411,412,413,414,415"]. Obviously, wordpress can parse this to retrieve all the images and display them in a gallery format using the "gallery_shortcode" function included in the core of wordpress (in includes/media.php). To get the ids of all images in the [gallery] short code I made a new function in my functions.php file:

function grab_ids_from_gallery() {

global $post;
$attachment_ids = array();
$pattern = get_shortcode_regex();
$ids = array();

if (preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) ) {   //finds the     "gallery" shortcode and puts the image ids in an associative array at $matches[3]
$count=count($matches[3]);      //in case there is more than one gallery in the post.
for ($i = 0; $i < $count; $i++){
    $atts = shortcode_parse_atts( $matches[3][$i] );
    if ( isset( $atts['ids'] ) ){
    $attachment_ids = explode( ',', $atts['ids'] );
    $ids = array_merge($ids, $attachment_ids);
    }
}
}
  return $ids;

 }
add_action( 'wp', 'grab_ids_from_gallery' );

Now just use the "grab_ids_from_gallery()" function to return all the ids as an array in your template.

I'm sure there is a more elegant way to do this - But this works for me. The basis for this code came from:

http://core.trac.wordpress.org/ticket/22960

which discusses this issue.