Title: get_attachment_template
Published: April 25, 2014
Last modified: May 20, 2026

---

# get_attachment_template(): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#see-also)
 * [Return](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#wp--skip-link--target)

Retrieves path of attachment template in current or parent template.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#description)󠁿

The hierarchy for this template looks like:

 1. {mime_type}-{sub_type}.php
 2. {sub_type}.php
 3. {mime_type}.php
 4. attachment.php

An example of this is:

 1. image-jpeg.php
 2. jpeg.php
 3. image.php
 4. attachment.php

The template hierarchy and template path are filterable via the [‘$type_template_hierarchy’](https://developer.wordpress.org/reference/hooks/type_template_hierarchy/)
and [‘$type_template’](https://developer.wordpress.org/reference/hooks/type_template/)
dynamic hooks, where `$type` is ‘attachment’.

### 󠀁[See also](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#see-also)󠁿

 * [get_query_template()](https://developer.wordpress.org/reference/functions/get_query_template/)

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#return)󠁿

 string Full path to attachment template file.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#source)󠁿

    ```php
    function get_attachment_template() {
    	$attachment = get_queried_object();

    	$templates = array();

    	if ( $attachment ) {
    		if ( str_contains( $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";
    		}
    		$templates[] = "{$type}.php";
    	}
    	$templates[] = 'attachment.php';

    	return get_query_template( 'attachment', $templates );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/template.php#L663)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/template.php#L663-L684)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#related)󠁿

| Uses | Description | 
| [get_queried_object()](https://developer.wordpress.org/reference/functions/get_queried_object/)`wp-includes/query.php` |

Retrieves the currently queried object.

  | 
| [get_query_template()](https://developer.wordpress.org/reference/functions/get_query_template/)`wp-includes/template.php` |

Retrieves path to a template.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_attachment_template/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.3.0](https://developer.wordpress.org/reference/since/4.3.0/) | The order of the mime type logic was reversed so the hierarchy is more logical. | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_attachment_template%2F)
before being able to contribute a note or feedback.