Using WordPress ‘get_query_template()’ PHP function

The get_query_template() WordPress PHP function retrieves the path to a specific template without including the file extension.

Usage

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

Custom example:

$template_path = get_query_template('archive');
echo $template_path; // Output: /path/to/your/theme/archive.php

Parameters

  • $type (string) – The filename without extension.
  • $templates (array) – An optional list of template candidates. Default: array().

More information

See WordPress Developer Resources: get_query_template

Examples

Get the path to the 404 template

This example retrieves the path to the 404.php template file.

$path_404 = get_query_template('404');
echo $path_404; // Output: /path/to/your/theme/404.php

Get the path to a custom template

This example retrieves the path to a custom template named custom-page.php.

$custom_template_path = get_query_template('custom-page');
echo $custom_template_path; // Output: /path/to/your/theme/custom-page.php

Get the path to the single post template

This example retrieves the path to the single.php template file.

$single_template_path = get_query_template('single');
echo $single_template_path; // Output: /path/to/your/theme/single.php

Get the path to the page template

This example retrieves the path to the page.php template file.

$page_template_path = get_query_template('page');
echo $page_template_path; // Output: /path/to/your/theme/page.php

Get the path to the search results template

This example retrieves the path to the search.php template file.

$search_template_path = get_query_template('search');
echo $search_template_path; // Output: /path/to/your/theme/search.php