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 an embed template path in the current or parent template.
410 * By default the WordPress-template is returned.
412 * The template path is filterable via the dynamic {@see '$type_template'} hook,
413 * e.g. 'embed_template'.
417 * @see get_query_template()
419 * @return string Full path to embed template file.
421 function get_embed_template() {
422 $object = get_queried_object();
424 $templates = array();
426 if ( ! empty( $object->post_type ) ) {
427 $post_format = get_post_format( $object );
428 if ( $post_format ) {
429 $templates[] = "embed-{$object->post_type}-{$post_format}.php";
431 $templates[] = "embed-{$object->post_type}.php";
434 $templates[] = "embed.php";
436 return get_query_template( 'embed', $templates );
440 * Retrieves the path of the singular template in current or parent template.
442 * The template path is filterable via the dynamic {@see '$type_template'} hook,
443 * e.g. 'singular_template'.
447 * @see get_query_template()
449 * @return string Full path to singular template file
451 function get_singular_template() {
452 return get_query_template( 'singular' );
456 * Retrieve path of attachment template in current or parent template.
458 * The attachment path first checks if the first part of the mime type exists.
459 * The second check is for the second part of the mime type. The last check is
460 * for both types separated by an underscore. If neither are found then the file
461 * 'attachment.php' is checked and returned.
463 * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
464 * finally 'text-plain.php'.
466 * The template path is filterable via the dynamic {@see '$type_template'} hook,
467 * e.g. 'attachment_template'.
471 * @see get_query_template()
473 * @global array $posts
475 * @return string Full path to attachment template file.
477 function get_attachment_template() {
478 $attachment = get_queried_object();
480 $templates = array();
483 if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
484 list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
486 list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
489 if ( ! empty( $subtype ) ) {
490 $templates[] = "{$type}-{$subtype}.php";
491 $templates[] = "{$subtype}.php";
493 $templates[] = "{$type}.php";
495 $templates[] = 'attachment.php';
497 return get_query_template( 'attachment', $templates );
501 * Retrieve the name of the highest priority template file that exists.
503 * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
504 * so that themes which inherit from a parent theme can just overload one file.
508 * @param string|array $template_names Template file(s) to search for, in order.
509 * @param bool $load If true the template file will be loaded if it is found.
510 * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
511 * @return string The template filename if one is located.
513 function locate_template($template_names, $load = false, $require_once = true ) {
515 foreach ( (array) $template_names as $template_name ) {
516 if ( !$template_name )
518 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
519 $located = STYLESHEETPATH . '/' . $template_name;
521 } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
522 $located = TEMPLATEPATH . '/' . $template_name;
524 } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
525 $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
530 if ( $load && '' != $located )
531 load_template( $located, $require_once );
537 * Require the template file with WordPress environment.
539 * The globals are set up for the template file to ensure that the WordPress
540 * environment is available from within the function. The query variables are
545 * @global array $posts
546 * @global WP_Post $post
547 * @global bool $wp_did_header
548 * @global WP_Query $wp_query
549 * @global WP_Rewrite $wp_rewrite
551 * @global string $wp_version
554 * @global WP_Comment $comment
555 * @global int $user_ID
557 * @param string $_template_file Path to template file.
558 * @param bool $require_once Whether to require_once or require. Default true.
560 function load_template( $_template_file, $require_once = true ) {
561 global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
563 if ( is_array( $wp_query->query_vars ) ) {
564 extract( $wp_query->query_vars, EXTR_SKIP );
571 if ( $require_once ) {
572 require_once( $_template_file );
574 require( $_template_file );