- global $posts;
-
- if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) ) {
- $type = explode( '/', $posts[0]->post_mime_type );
-
- if ( ! empty( $type ) ) {
- if ( $template = get_query_template( $type[0] ) )
- return $template;
- elseif ( ! empty( $type[1] ) ) {
- if ( $template = get_query_template( $type[1] ) )
- return $template;
- elseif ( $template = get_query_template( "$type[0]_$type[1]" ) )
- return $template;
- }
+ $attachment = get_queried_object();
+
+ $templates = array();
+
+ if ( $attachment ) {
+ if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
+ list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
+ } else {
+ list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
+ }
+
+ if ( ! empty( $subtype ) ) {
+ $templates[] = "{$type}-{$subtype}.php";
+ $templates[] = "{$subtype}.php";