get_query_template

Retrieve path to a template.

Description

(string) get_query_template( (string) $type, (array) $templates = array() ); 

Used to quickly retrieve the path of a template without including the file extension. It will also check the parent theme, if the file exists, with the use of locate_template(). Allows for more generic template location without the use of the other get_*_template() functions.

Returns (string)

Full path to template file.

Parameters (2)

0. $type (string)
Filename without extension.
1. $templates — Optional. (array) => array()
An optional list of template candidates

Usage

  1. if ( !function_exists( 'get_query_template' ) ) { 
  2. require_once ABSPATH . WPINC . '/template.php'; 
  3.  
  4. // Filename without extension. 
  5. $type = ''; 
  6.  
  7. // An optional list of template candidates 
  8. $templates = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_query_template($type, $templates); 
  12.  

Defined (1)

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

/wp-includes/template.php  
  1. function get_query_template( $type, $templates = array() ) { 
  2. $type = preg_replace( '|[^a-z0-9-]+|', '', $type ); 
  3.  
  4. if ( empty( $templates ) ) 
  5. $templates = array("{$type}.php"); 
  6.  
  7. /** 
  8. * Filters the list of template filenames that are searched for when retrieving a template to use. 
  9. * The last element in the array should always be the fallback template for this query type. 
  10. * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',  
  11. * 'embed', home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'. 
  12. * @since 4.7.0 
  13. * @param array $templates A list of template candidates, in descending order of priority. 
  14. */ 
  15. $templates = apply_filters( "{$type}_template_hierarchy", $templates ); 
  16.  
  17. $template = locate_template( $templates ); 
  18.  
  19. /** 
  20. * Filters the path of the queried template by type. 
  21. * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file 
  22. * extension and any non-alphanumeric characters delimiting words -- of the file to load. 
  23. * This hook also applies to various types of files loaded as part of the Template Hierarchy. 
  24. * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',  
  25. * 'embed', home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'. 
  26. * @since 1.5.0 
  27. * @param string $template Path to the template. See locate_template(). 
  28. */ 
  29. return apply_filters( "{$type}_template", $template );