get_single_template

Retrieve path of single template in current or parent template.

Description

(string) get_single_template(); 

Applies to single Posts, single Attachments, and single custom post types.

The hierarchy for this template looks like:

1. .php 2. single--.php 3. single-.php 4. single.php

An example of this is:

1. templates/full-width.php 2. single-post-hello-world.php 3. single-post.php 4. single.php

The template hierarchy is filterable via the hook. The template path is filterable via the hook.

Returns (string)

Full path to single template file.


Usage

  1. if ( !function_exists( 'get_single_template' ) ) { 
  2. require_once ABSPATH . WPINC . '/template.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = get_single_template(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/template.php  
  1. function get_single_template() { 
  2. $object = get_queried_object(); 
  3.  
  4. $templates = array(); 
  5.  
  6. if ( ! empty( $object->post_type ) ) { 
  7. $template = get_page_template_slug( $object ); 
  8. if ( $template && 0 === validate_file( $template ) ) { 
  9. $templates[] = $template; 
  10.  
  11. $name_decoded = urldecode( $object->post_name ); 
  12. if ( $name_decoded !== $object->post_name ) { 
  13. $templates[] = "single-{$object->post_type}-{$name_decoded}.php"; 
  14.  
  15. $templates[] = "single-{$object->post_type}-{$object->post_name}.php"; 
  16. $templates[] = "single-{$object->post_type}.php"; 
  17.  
  18. $templates[] = "single.php"; 
  19.  
  20. return get_query_template( 'single', $templates );