get_single_template()
Retrieves path of single template in current or parent template. Applies to single Posts, single Attachments, and single custom post types.
The hierarchy for this template looks like:
- {Post Type Template}.php
- single-{post_type}-{post_name}.php
- single-{post_type}.php
- single.php
An example of this is:
- templates/full-width.php
- single-post-hello-world.php
- single-post.php
- single.php
The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'} and {@see '$type_template'} dynamic hooks, where $type is 'single'.
No Hooks.
Returns
String. Full path to single template file.
Usage
get_single_template();
Notes
- See: get_query_template()
Changelog
| Since 1.5.0 | Introduced. |
| Since 4.4.0 | single-{post_type}-{post_name}.php was added to the top of the template hierarchy. |
| Since 4.7.0 | The decoded form of single-{post_type}-{post_name}.php was added to the top of the template hierarchy when the post name contains multibyte characters. |
| Since 4.7.0 | {Post Type Template}.php was added to the top of the template hierarchy. |
get_single_template() get single template code WP 6.8.3
function get_single_template() {
$object = get_queried_object();
$templates = array();
if ( ! empty( $object->post_type ) ) {
$template = get_page_template_slug( $object );
if ( $template && 0 === validate_file( $template ) ) {
$templates[] = $template;
}
$name_decoded = urldecode( $object->post_name );
if ( $name_decoded !== $object->post_name ) {
$templates[] = "single-{$object->post_type}-{$name_decoded}.php";
}
$templates[] = "single-{$object->post_type}-{$object->post_name}.php";
$templates[] = "single-{$object->post_type}.php";
}
$templates[] = 'single.php';
return get_query_template( 'single', $templates );
}