3 * Template loading functions.
10 * Retrieve path to a template
12 * Used to quickly retrieve the path of a template without including the file
13 * extension. It will also check the parent theme, if the file exists, with
14 * the use of {@link locate_template()}. Allows for more generic template location
15 * without the use of the other get_*_template() functions.
19 * @param string $type Filename without extension.
20 * @param array $templates An optional list of template candidates
21 * @return string Full path to template file.
23 function get_query_template( $type, $templates = array() ) {
24 $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
26 if ( empty( $templates ) )
27 $templates = array("{$type}.php");
29 $template = locate_template( $templates );
32 * Filter the path of the queried template by type.
34 * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
35 * extension and any non-alphanumeric characters delimiting words -- of the file to load.
36 * This hook also applies to various types of files loaded as part of the Template Hierarchy.
38 * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
39 * 'home', 'front_page', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
43 * @param string $template Path to the template. See locate_template().
45 return apply_filters( "{$type}_template", $template );
49 * Retrieve path of index template in current or parent template.
51 * The template path is filterable via the dynamic {@see '$type_template'} hook,
52 * e.g. 'index_template'.
56 * @see get_query_template()
58 * @return string Full path to index template file.
60 function get_index_template() {
61 return get_query_template('index');
65 * Retrieve path of 404 template in current or parent template.
67 * The template path is filterable via the dynamic {@see '$type_template'} hook,
68 * e.g. '404_template'.
72 * @see get_query_template()
74 * @return string Full path to 404 template file.
76 function get_404_template() {
77 return get_query_template('404');
81 * Retrieve path of archive template in current or parent template.
83 * The template path is filterable via the dynamic {@see '$type_template'} hook,
84 * e.g. 'archive_template'.
88 * @see get_query_template()
90 * @return string Full path to archive template file.
92 function get_archive_template() {
93 $post_types = array_filter( (array) get_query_var( 'post_type' ) );
97 if ( count( $post_types ) == 1 ) {
98 $post_type = reset( $post_types );
99 $templates[] = "archive-{$post_type}.php";
101 $templates[] = 'archive.php';
103 return get_query_template( 'archive', $templates );
107 * Retrieve path of post type archive template in current or parent template.
109 * The template path is filterable via the dynamic {@see '$type_template'} hook,
110 * e.g. 'archive_template'.
114 * @see get_archive_template()
116 * @return string Full path to archive template file.
118 function get_post_type_archive_template() {
119 $post_type = get_query_var( 'post_type' );
120 if ( is_array( $post_type ) )
121 $post_type = reset( $post_type );
123 $obj = get_post_type_object( $post_type );
124 if ( ! $obj->has_archive )
127 return get_archive_template();
131 * Retrieve path of author template in current or parent template.
133 * The template path is filterable via the dynamic {@see '$type_template'} hook,
134 * e.g. 'author_template'.
138 * @see get_query_template()
140 * @return string Full path to author template file.
142 function get_author_template() {
143 $author = get_queried_object();
145 $templates = array();
147 if ( $author instanceof WP_User ) {
148 $templates[] = "author-{$author->user_nicename}.php";
149 $templates[] = "author-{$author->ID}.php";
151 $templates[] = 'author.php';
153 return get_query_template( 'author', $templates );
157 * Retrieve path of category template in current or parent template.
159 * Works by first retrieving the current slug, for example 'category-default.php',
160 * and then trying category ID, for example 'category-1.php', and will finally fall
161 * back to category.php template, if those files don't exist.
163 * The template path is filterable via the dynamic {@see '$type_template'} hook,
164 * e.g. 'category_template'.
168 * @see get_query_template()
170 * @return string Full path to category template file.
172 function get_category_template() {
173 $category = get_queried_object();
175 $templates = array();
177 if ( ! empty( $category->slug ) ) {
178 $templates[] = "category-{$category->slug}.php";
179 $templates[] = "category-{$category->term_id}.php";
181 $templates[] = 'category.php';
183 return get_query_template( 'category', $templates );
187 * Retrieve path of tag template in current or parent template.
189 * Works by first retrieving the current tag name, for example 'tag-wordpress.php',
190 * and then trying tag ID, for example 'tag-1.php', and will finally fall back to
191 * tag.php template, if those files don't exist.
193 * The template path is filterable via the dynamic {@see '$type_template'} hook,
194 * e.g. 'tag_template'.
198 * @see get_query_template()
200 * @return string Full path to tag template file.
202 function get_tag_template() {
203 $tag = get_queried_object();
205 $templates = array();
207 if ( ! empty( $tag->slug ) ) {
208 $templates[] = "tag-{$tag->slug}.php";
209 $templates[] = "tag-{$tag->term_id}.php";
211 $templates[] = 'tag.php';
213 return get_query_template( 'tag', $templates );
217 * Retrieve path of taxonomy template in current or parent template.
219 * Retrieves the taxonomy and term, if term is available. The template is
220 * prepended with 'taxonomy-' and followed by both the taxonomy string and
221 * the taxonomy string followed by a dash and then followed by the term.
223 * The taxonomy and term template is checked and used first, if it exists.
224 * Second, just the taxonomy template is checked, and then finally, taxonomy.php
225 * template is used. If none of the files exist, then it will fall back on to
228 * The template path is filterable via the dynamic {@see '$type_template'} hook,
229 * e.g. 'taxonomy_template'.
233 * @see get_query_template()
235 * @return string Full path to taxonomy template file.
237 function get_taxonomy_template() {
238 $term = get_queried_object();
240 $templates = array();
242 if ( ! empty( $term->slug ) ) {
243 $taxonomy = $term->taxonomy;
244 $templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
245 $templates[] = "taxonomy-$taxonomy.php";
247 $templates[] = 'taxonomy.php';
249 return get_query_template( 'taxonomy', $templates );
253 * Retrieve path of date template in current or parent template.
255 * The template path is filterable via the dynamic {@see '$type_template'} hook,
256 * e.g. 'date_template'.
260 * @see get_query_template()
262 * @return string Full path to date template file.
264 function get_date_template() {
265 return get_query_template('date');
269 * Retrieve path of home template in current or parent template.
271 * This is the template used for the page containing the blog posts.
272 * Attempts to locate 'home.php' first before falling back to 'index.php'.
274 * The template path is filterable via the dynamic {@see '$type_template'} hook,
275 * e.g. 'home_template'.
279 * @see get_query_template()
281 * @return string Full path to home template file.
283 function get_home_template() {
284 $templates = array( 'home.php', 'index.php' );
286 return get_query_template( 'home', $templates );
290 * Retrieve path of front-page template in current or parent template.
292 * Looks for 'front-page.php'. The template path is filterable via the
293 * dynamic {@see '$type_template'} hook, e.g. 'frontpage_template'.
297 * @see get_query_template()
299 * @return string Full path to front page template file.
301 function get_front_page_template() {
302 $templates = array('front-page.php');
304 return get_query_template( 'front_page', $templates );
308 * Retrieve path of page template in current or parent template.
310 * Will first look for the specifically assigned page template.
311 * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
312 * and finally 'page.php'.
314 * The template path is filterable via the dynamic {@see '$type_template'} hook,
315 * e.g. 'page_template'.
319 * @see get_query_template()
321 * @return string Full path to page template file.
323 function get_page_template() {
324 $id = get_queried_object_id();
325 $template = get_page_template_slug();
326 $pagename = get_query_var('pagename');
328 if ( ! $pagename && $id ) {
329 // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
330 $post = get_queried_object();
332 $pagename = $post->post_name;
335 $templates = array();
336 if ( $template && 0 === validate_file( $template ) )
337 $templates[] = $template;
339 $templates[] = "page-$pagename.php";
341 $templates[] = "page-$id.php";
342 $templates[] = 'page.php';
344 return get_query_template( 'page', $templates );
348 * Retrieve path of paged template in current or parent template.
350 * The template path is filterable via the dynamic {@see '$type_template'} hook,
351 * e.g. 'paged_template'.
355 * @see get_query_template()
357 * @return string Full path to paged template file.
359 function get_paged_template() {
360 return get_query_template('paged');
364 * Retrieve path of search template in current or parent template.
366 * The template path is filterable via the dynamic {@see '$type_template'} hook,
367 * e.g. 'search_template'.
371 * @see get_query_template()
373 * @return string Full path to search template file.
375 function get_search_template() {
376 return get_query_template('search');
380 * Retrieve path of single template in current or parent template.
382 * The template path is filterable via the dynamic {@see '$type_template'} hook,
383 * e.g. 'single_template'.
386 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
388 * @see get_query_template()
390 * @return string Full path to single template file.
392 function get_single_template() {
393 $object = get_queried_object();
395 $templates = array();
397 if ( ! empty( $object->post_type ) ) {
398 $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
399 $templates[] = "single-{$object->post_type}.php";
402 $templates[] = "single.php";
404 return get_query_template( 'single', $templates );
408 * Retrieves the path of the singular template in current or parent template.
410 * The template path is filterable via the dynamic {@see '$type_template'} hook,
411 * e.g. 'singular_template'.
415 * @see get_query_template()
417 * @return string Full path to singular template file
419 function get_singular_template() {
420 return get_query_template( 'singular' );
424 * Retrieve path of attachment template in current or parent template.
426 * The attachment path first checks if the first part of the mime type exists.
427 * The second check is for the second part of the mime type. The last check is
428 * for both types separated by an underscore. If neither are found then the file
429 * 'attachment.php' is checked and returned.
431 * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
432 * finally 'text-plain.php'.
434 * The template path is filterable via the dynamic {@see '$type_template'} hook,
435 * e.g. 'attachment_template'.
439 * @see get_query_template()
441 * @global array $posts
443 * @return string Full path to attachment template file.
445 function get_attachment_template() {
446 $attachment = get_queried_object();
448 $templates = array();
451 if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
452 list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
454 list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
457 if ( ! empty( $subtype ) ) {
458 $templates[] = "{$type}-{$subtype}.php";
459 $templates[] = "{$subtype}.php";
461 $templates[] = "{$type}.php";
463 $templates[] = 'attachment.php';
465 return get_query_template( 'attachment', $templates );
469 * Retrieve path of comment popup template in current or parent template.
471 * Checks for comment popup template in current template, if it exists or in the
474 * The template path is filterable via the dynamic {@see '$type_template'} hook,
475 * e.g. 'commentspopup_template'.
479 * @see get_query_template()
481 * @return string Full path to comments popup template file.
483 function get_comments_popup_template() {
484 $template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );
486 // Backward compat code will be removed in a future release.
488 $template = ABSPATH . WPINC . '/theme-compat/comments-popup.php';
494 * Retrieve the name of the highest priority template file that exists.
496 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
497 * inherit from a parent theme can just overload one file.
501 * @param string|array $template_names Template file(s) to search for, in order.
502 * @param bool $load If true the template file will be loaded if it is found.
503 * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
504 * @return string The template filename if one is located.
506 function locate_template($template_names, $load = false, $require_once = true ) {
508 foreach ( (array) $template_names as $template_name ) {
509 if ( !$template_name )
511 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
512 $located = STYLESHEETPATH . '/' . $template_name;
514 } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
515 $located = TEMPLATEPATH . '/' . $template_name;
520 if ( $load && '' != $located )
521 load_template( $located, $require_once );
527 * Require the template file with WordPress environment.
529 * The globals are set up for the template file to ensure that the WordPress
530 * environment is available from within the function. The query variables are
535 * @global array $posts
536 * @global WP_Post $post
537 * @global bool $wp_did_header
538 * @global WP_Query $wp_query
539 * @global WP_Rewrite $wp_rewrite
541 * @global string $wp_version
544 * @global WP_Comment $comment
545 * @global int $user_ID
547 * @param string $_template_file Path to template file.
548 * @param bool $require_once Whether to require_once or require. Default true.
550 function load_template( $_template_file, $require_once = true ) {
551 global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
553 if ( is_array( $wp_query->query_vars ) ) {
554 extract( $wp_query->query_vars, EXTR_SKIP );
561 if ( $require_once ) {
562 require_once( $_template_file );
564 require( $_template_file );