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 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");
30 * Filters the list of template filenames that are searched for when retrieving a template to use.
32 * The last element in the array should always be the fallback template for this query type.
34 * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
35 * 'embed', home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
39 * @param array $templates A list of template candidates, in descending order of priority.
41 $templates = apply_filters( "{$type}_template_hierarchy", $templates );
43 $template = locate_template( $templates );
46 * Filters the path of the queried template by type.
48 * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
49 * extension and any non-alphanumeric characters delimiting words -- of the file to load.
50 * This hook also applies to various types of files loaded as part of the Template Hierarchy.
52 * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
53 * 'embed', home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
57 * @param string $template Path to the template. See locate_template().
59 return apply_filters( "{$type}_template", $template );
63 * Retrieve path of index template in current or parent template.
65 * The template hierarchy is filterable via the {@see 'index_template_hierarchy'} hook.
66 * The template path is filterable via the {@see 'index_template'} hook.
70 * @see get_query_template()
72 * @return string Full path to index template file.
74 function get_index_template() {
75 return get_query_template('index');
79 * Retrieve path of 404 template in current or parent template.
81 * The template hierarchy is filterable via the {@see '404_template_hierarchy'} hook.
82 * The template path is filterable via the {@see '404_template'} hook.
86 * @see get_query_template()
88 * @return string Full path to 404 template file.
90 function get_404_template() {
91 return get_query_template('404');
95 * Retrieve path of archive template in current or parent template.
97 * The template hierarchy is filterable via the {@see 'archive_template_hierarchy'} hook.
98 * The template path is filterable via the {@see 'archive_template'} hook.
102 * @see get_query_template()
104 * @return string Full path to archive template file.
106 function get_archive_template() {
107 $post_types = array_filter( (array) get_query_var( 'post_type' ) );
109 $templates = array();
111 if ( count( $post_types ) == 1 ) {
112 $post_type = reset( $post_types );
113 $templates[] = "archive-{$post_type}.php";
115 $templates[] = 'archive.php';
117 return get_query_template( 'archive', $templates );
121 * Retrieve path of post type archive template in current or parent template.
123 * The template hierarchy is filterable via the {@see 'archive_template_hierarchy'} hook.
124 * The template path is filterable via the {@see 'archive_template'} hook.
128 * @see get_archive_template()
130 * @return string Full path to archive template file.
132 function get_post_type_archive_template() {
133 $post_type = get_query_var( 'post_type' );
134 if ( is_array( $post_type ) )
135 $post_type = reset( $post_type );
137 $obj = get_post_type_object( $post_type );
138 if ( ! $obj->has_archive )
141 return get_archive_template();
145 * Retrieve path of author template in current or parent template.
147 * The hierarchy for this template looks like:
149 * 1. author-{nicename}.php
153 * An example of this is:
159 * The template hierarchy is filterable via the {@see 'author_template_hierarchy'} hook.
160 * The template path is filterable via the {@see 'author_template'} hook.
164 * @see get_query_template()
166 * @return string Full path to author template file.
168 function get_author_template() {
169 $author = get_queried_object();
171 $templates = array();
173 if ( $author instanceof WP_User ) {
174 $templates[] = "author-{$author->user_nicename}.php";
175 $templates[] = "author-{$author->ID}.php";
177 $templates[] = 'author.php';
179 return get_query_template( 'author', $templates );
183 * Retrieve path of category template in current or parent template.
185 * The hierarchy for this template looks like:
187 * 1. category-{slug}.php
188 * 2. category-{id}.php
191 * An example of this is:
193 * 1. category-news.php
197 * The template hierarchy is filterable via the {@see 'category_template_hierarchy'} hook.
198 * The template path is filterable via the {@see 'category_template'} hook.
201 * @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
202 * template hierarchy when the category slug contains multibyte characters.
204 * @see get_query_template()
206 * @return string Full path to category template file.
208 function get_category_template() {
209 $category = get_queried_object();
211 $templates = array();
213 if ( ! empty( $category->slug ) ) {
215 $slug_decoded = urldecode( $category->slug );
216 if ( $slug_decoded !== $category->slug ) {
217 $templates[] = "category-{$slug_decoded}.php";
220 $templates[] = "category-{$category->slug}.php";
221 $templates[] = "category-{$category->term_id}.php";
223 $templates[] = 'category.php';
225 return get_query_template( 'category', $templates );
229 * Retrieve path of tag template in current or parent template.
231 * The hierarchy for this template looks like:
237 * An example of this is:
239 * 1. tag-wordpress.php
243 * The template hierarchy is filterable via the {@see 'tag_template_hierarchy'} hook.
244 * The template path is filterable via the {@see 'tag_template'} hook.
247 * @since 4.7.0 The decoded form of `tag-{slug}.php` was added to the top of the
248 * template hierarchy when the tag slug contains multibyte characters.
250 * @see get_query_template()
252 * @return string Full path to tag template file.
254 function get_tag_template() {
255 $tag = get_queried_object();
257 $templates = array();
259 if ( ! empty( $tag->slug ) ) {
261 $slug_decoded = urldecode( $tag->slug );
262 if ( $slug_decoded !== $tag->slug ) {
263 $templates[] = "tag-{$slug_decoded}.php";
266 $templates[] = "tag-{$tag->slug}.php";
267 $templates[] = "tag-{$tag->term_id}.php";
269 $templates[] = 'tag.php';
271 return get_query_template( 'tag', $templates );
275 * Retrieve path of custom taxonomy term template in current or parent template.
277 * The hierarchy for this template looks like:
279 * 1. taxonomy-{taxonomy_slug}-{term_slug}.php
280 * 2. taxonomy-{taxonomy_slug}.php
283 * An example of this is:
285 * 1. taxonomy-location-texas.php
286 * 2. taxonomy-location.php
289 * The template hierarchy is filterable via the {@see 'taxonomy_template_hierarchy'} hook.
290 * The template path is filterable via the {@see 'taxonomy_template'} hook.
293 * @since 4.7.0 The decoded form of `taxonomy-{taxonomy_slug}-{term_slug}.php` was added to the top of the
294 * template hierarchy when the term slug contains multibyte characters.
296 * @see get_query_template()
298 * @return string Full path to custom taxonomy term template file.
300 function get_taxonomy_template() {
301 $term = get_queried_object();
303 $templates = array();
305 if ( ! empty( $term->slug ) ) {
306 $taxonomy = $term->taxonomy;
308 $slug_decoded = urldecode( $term->slug );
309 if ( $slug_decoded !== $term->slug ) {
310 $templates[] = "taxonomy-$taxonomy-{$slug_decoded}.php";
313 $templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
314 $templates[] = "taxonomy-$taxonomy.php";
316 $templates[] = 'taxonomy.php';
318 return get_query_template( 'taxonomy', $templates );
322 * Retrieve path of date template in current or parent template.
324 * The template hierarchy is filterable via the {@see 'date_template_hierarchy'} hook.
325 * The template path is filterable via the {@see 'date_template'} hook.
329 * @see get_query_template()
331 * @return string Full path to date template file.
333 function get_date_template() {
334 return get_query_template('date');
338 * Retrieve path of home template in current or parent template.
340 * The template hierarchy is filterable via the {@see 'home_template_hierarchy'} hook.
341 * The template path is filterable via the {@see 'home_template'} hook.
345 * @see get_query_template()
347 * @return string Full path to home template file.
349 function get_home_template() {
350 $templates = array( 'home.php', 'index.php' );
352 return get_query_template( 'home', $templates );
356 * Retrieve path of front page template in current or parent template.
358 * The template hierarchy is filterable via the {@see 'frontpage_template_hierarchy'} hook.
359 * The template path is filterable via the {@see 'frontpage_template'} hook.
363 * @see get_query_template()
365 * @return string Full path to front page template file.
367 function get_front_page_template() {
368 $templates = array('front-page.php');
370 return get_query_template( 'front_page', $templates );
374 * Retrieve path of page template in current or parent template.
376 * The hierarchy for this template looks like:
378 * 1. {Page Template}.php
379 * 2. page-{page_name}.php
383 * An example of this is:
385 * 1. page-templates/full-width.php
390 * The template hierarchy is filterable via the {@see 'page_template_hierarchy'} hook.
391 * The template path is filterable via the {@see 'page_template'} hook.
394 * @since 4.7.0 The decoded form of `page-{page_name}.php` was added to the top of the
395 * template hierarchy when the page name contains multibyte characters.
397 * @see get_query_template()
399 * @return string Full path to page template file.
401 function get_page_template() {
402 $id = get_queried_object_id();
403 $template = get_page_template_slug();
404 $pagename = get_query_var('pagename');
406 if ( ! $pagename && $id ) {
407 // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
408 $post = get_queried_object();
410 $pagename = $post->post_name;
413 $templates = array();
414 if ( $template && 0 === validate_file( $template ) )
415 $templates[] = $template;
417 $pagename_decoded = urldecode( $pagename );
418 if ( $pagename_decoded !== $pagename ) {
419 $templates[] = "page-{$pagename_decoded}.php";
421 $templates[] = "page-$pagename.php";
424 $templates[] = "page-$id.php";
425 $templates[] = 'page.php';
427 return get_query_template( 'page', $templates );
431 * Retrieve path of search template in current or parent template.
433 * The template hierarchy is filterable via the {@see 'search_template_hierarchy'} hook.
434 * The template path is filterable via the {@see 'search_template'} hook.
438 * @see get_query_template()
440 * @return string Full path to search template file.
442 function get_search_template() {
443 return get_query_template('search');
447 * Retrieve path of single template in current or parent template. Applies to single Posts,
448 * single Attachments, and single custom post types.
450 * The hierarchy for this template looks like:
452 * 1. {Post Type Template}.php
453 * 2. single-{post_type}-{post_name}.php
454 * 3. single-{post_type}.php
457 * An example of this is:
459 * 1. templates/full-width.php
460 * 2. single-post-hello-world.php
464 * The template hierarchy is filterable via the {@see 'single_template_hierarchy'} hook.
465 * The template path is filterable via the {@see 'single_template'} hook.
468 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
469 * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
470 * template hierarchy when the post name contains multibyte characters.
471 * @since 4.7.0 {Post Type Template}.php was added to the top of the template hierarchy.
473 * @see get_query_template()
475 * @return string Full path to single template file.
477 function get_single_template() {
478 $object = get_queried_object();
480 $templates = array();
482 if ( ! empty( $object->post_type ) ) {
483 $template = get_page_template_slug( $object );
484 if ( $template && 0 === validate_file( $template ) ) {
485 $templates[] = $template;
488 $name_decoded = urldecode( $object->post_name );
489 if ( $name_decoded !== $object->post_name ) {
490 $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
493 $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
494 $templates[] = "single-{$object->post_type}.php";
497 $templates[] = "single.php";
499 return get_query_template( 'single', $templates );
503 * Retrieves an embed template path in the current or parent template.
505 * The hierarchy for this template looks like:
507 * 1. embed-{post_type}-{post_format}.php
508 * 2. embed-{post_type}.php
511 * An example of this is:
513 * 1. embed-post-audio.php
517 * The template hierarchy is filterable via the {@see 'embed_template_hierarchy'} hook.
518 * The template path is filterable via the {@see 'embed_template'} hook.
522 * @see get_query_template()
524 * @return string Full path to embed template file.
526 function get_embed_template() {
527 $object = get_queried_object();
529 $templates = array();
531 if ( ! empty( $object->post_type ) ) {
532 $post_format = get_post_format( $object );
533 if ( $post_format ) {
534 $templates[] = "embed-{$object->post_type}-{$post_format}.php";
536 $templates[] = "embed-{$object->post_type}.php";
539 $templates[] = "embed.php";
541 return get_query_template( 'embed', $templates );
545 * Retrieves the path of the singular template in current or parent template.
547 * The template hierarchy is filterable via the {@see 'singular_template_hierarchy'} hook.
548 * The template path is filterable via the {@see 'singular_template'} hook.
552 * @see get_query_template()
554 * @return string Full path to singular template file
556 function get_singular_template() {
557 return get_query_template( 'singular' );
561 * Retrieve path of attachment template in current or parent template.
563 * The hierarchy for this template looks like:
565 * 1. {mime_type}-{sub_type}.php
570 * An example of this is:
577 * The template hierarchy is filterable via the {@see 'attachment_template_hierarchy'} hook.
578 * The template path is filterable via the {@see 'attachment_template'} hook.
581 * @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
583 * @see get_query_template()
585 * @global array $posts
587 * @return string Full path to attachment template file.
589 function get_attachment_template() {
590 $attachment = get_queried_object();
592 $templates = array();
595 if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
596 list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
598 list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
601 if ( ! empty( $subtype ) ) {
602 $templates[] = "{$type}-{$subtype}.php";
603 $templates[] = "{$subtype}.php";
605 $templates[] = "{$type}.php";
607 $templates[] = 'attachment.php';
609 return get_query_template( 'attachment', $templates );
613 * Retrieve the name of the highest priority template file that exists.
615 * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
616 * so that themes which inherit from a parent theme can just overload one file.
620 * @param string|array $template_names Template file(s) to search for, in order.
621 * @param bool $load If true the template file will be loaded if it is found.
622 * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
623 * @return string The template filename if one is located.
625 function locate_template($template_names, $load = false, $require_once = true ) {
627 foreach ( (array) $template_names as $template_name ) {
628 if ( !$template_name )
630 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
631 $located = STYLESHEETPATH . '/' . $template_name;
633 } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
634 $located = TEMPLATEPATH . '/' . $template_name;
636 } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
637 $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
642 if ( $load && '' != $located )
643 load_template( $located, $require_once );
649 * Require the template file with WordPress environment.
651 * The globals are set up for the template file to ensure that the WordPress
652 * environment is available from within the function. The query variables are
657 * @global array $posts
658 * @global WP_Post $post
659 * @global bool $wp_did_header
660 * @global WP_Query $wp_query
661 * @global WP_Rewrite $wp_rewrite
663 * @global string $wp_version
666 * @global WP_Comment $comment
667 * @global int $user_ID
669 * @param string $_template_file Path to template file.
670 * @param bool $require_once Whether to require_once or require. Default true.
672 function load_template( $_template_file, $require_once = true ) {
673 global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
675 if ( is_array( $wp_query->query_vars ) ) {
676 extract( $wp_query->query_vars, EXTR_SKIP );
683 if ( $require_once ) {
684 require_once( $_template_file );
686 require( $_template_file );