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 );
31 * Filter the path of the queried template by type.
33 * The dynamic portion of the hook name, `$type`, refers to the filename
34 * -- minus the extension -- of the file to load. This hook also applies
35 * to various types of files loaded as part of the Template Hierarchy.
39 * @param string $template Path to the template. See {@see locate_template()}.
41 return apply_filters( "{$type}_template", $template );
45 * Retrieve path of index template in current or parent template.
47 * The template path is filterable via the 'index_template' hook.
51 * @see get_query_template()
53 * @return string Full path to index template file.
55 function get_index_template() {
56 return get_query_template('index');
60 * Retrieve path of 404 template in current or parent template.
62 * The template path is filterable via the '404_template' hook.
66 * @see get_query_template()
68 * @return string Full path to 404 template file.
70 function get_404_template() {
71 return get_query_template('404');
75 * Retrieve path of archive template in current or parent template.
77 * The template path is filterable via the 'archive_template' hook.
81 * @see get_query_template()
83 * @return string Full path to archive template file.
85 function get_archive_template() {
86 $post_types = array_filter( (array) get_query_var( 'post_type' ) );
90 if ( count( $post_types ) == 1 ) {
91 $post_type = reset( $post_types );
92 $templates[] = "archive-{$post_type}.php";
94 $templates[] = 'archive.php';
96 return get_query_template( 'archive', $templates );
100 * Retrieve path of post type archive template in current or parent template.
102 * The template path is filterable via the 'archive_template' hook.
106 * @see get_archive_template()
108 * @return string Full path to archive template file.
110 function get_post_type_archive_template() {
111 $post_type = get_query_var( 'post_type' );
112 if ( is_array( $post_type ) )
113 $post_type = reset( $post_type );
115 $obj = get_post_type_object( $post_type );
116 if ( ! $obj->has_archive )
119 return get_archive_template();
123 * Retrieve path of author template in current or parent template.
125 * The template path is filterable via the 'author_template' hook.
129 * @see get_query_template()
131 * @return string Full path to author template file.
133 function get_author_template() {
134 $author = get_queried_object();
136 $templates = array();
138 if ( $author instanceof WP_User ) {
139 $templates[] = "author-{$author->user_nicename}.php";
140 $templates[] = "author-{$author->ID}.php";
142 $templates[] = 'author.php';
144 return get_query_template( 'author', $templates );
148 * Retrieve path of category template in current or parent template.
150 * Works by first retrieving the current slug, for example 'category-default.php', and then
151 * trying category ID, for example 'category-1.php', and will finally fall back to category.php
152 * template, if those files don't exist.
154 * The template path is filterable via the 'category_template' hook.
158 * @see get_query_template()
160 * @return string Full path to category template file.
162 function get_category_template() {
163 $category = get_queried_object();
165 $templates = array();
167 if ( ! empty( $category->slug ) ) {
168 $templates[] = "category-{$category->slug}.php";
169 $templates[] = "category-{$category->term_id}.php";
171 $templates[] = 'category.php';
173 return get_query_template( 'category', $templates );
177 * Retrieve path of tag template in current or parent template.
179 * Works by first retrieving the current tag name, for example 'tag-wordpress.php', and then
180 * trying tag ID, for example 'tag-1.php', and will finally fall back to tag.php
181 * template, if those files don't exist.
183 * The template path is filterable via the 'tag_template' hook.
187 * @see get_query_template()
189 * @return string Full path to tag template file.
191 function get_tag_template() {
192 $tag = get_queried_object();
194 $templates = array();
196 if ( ! empty( $tag->slug ) ) {
197 $templates[] = "tag-{$tag->slug}.php";
198 $templates[] = "tag-{$tag->term_id}.php";
200 $templates[] = 'tag.php';
202 return get_query_template( 'tag', $templates );
206 * Retrieve path of taxonomy template in current or parent template.
208 * Retrieves the taxonomy and term, if term is available. The template is
209 * prepended with 'taxonomy-' and followed by both the taxonomy string and
210 * the taxonomy string followed by a dash and then followed by the term.
212 * The taxonomy and term template is checked and used first, if it exists.
213 * Second, just the taxonomy template is checked, and then finally, taxonomy.php
214 * template is used. If none of the files exist, then it will fall back on to
217 * The template path is filterable via the 'taxonomy_template' hook.
221 * @see get_query_template()
223 * @return string Full path to taxonomy template file.
225 function get_taxonomy_template() {
226 $term = get_queried_object();
228 $templates = array();
230 if ( ! empty( $term->slug ) ) {
231 $taxonomy = $term->taxonomy;
232 $templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
233 $templates[] = "taxonomy-$taxonomy.php";
235 $templates[] = 'taxonomy.php';
237 return get_query_template( 'taxonomy', $templates );
241 * Retrieve path of date template in current or parent template.
243 * The template path is filterable via the 'date_template' hook.
247 * @see get_query_template()
249 * @return string Full path to date template file.
251 function get_date_template() {
252 return get_query_template('date');
256 * Retrieve path of home template in current or parent template.
258 * This is the template used for the page containing the blog posts.
259 * Attempts to locate 'home.php' first before falling back to 'index.php'.
261 * The template path is filterable via the 'home_template' hook.
265 * @see get_query_template()
267 * @return string Full path to home template file.
269 function get_home_template() {
270 $templates = array( 'home.php', 'index.php' );
272 return get_query_template( 'home', $templates );
276 * Retrieve path of front-page template in current or parent template.
278 * Looks for 'front-page.php'. The template path is filterable via the
279 * 'front_page_template' hook.
283 * @see get_query_template()
285 * @return string Full path to front page template file.
287 function get_front_page_template() {
288 $templates = array('front-page.php');
290 return get_query_template( 'front_page', $templates );
294 * Retrieve path of page template in current or parent template.
296 * Will first look for the specifically assigned page template.
297 * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
298 * and finally 'page.php'.
300 * The template path is filterable via the 'page_template' hook.
304 * @see get_query_template()
306 * @return string Full path to page template file.
308 function get_page_template() {
309 $id = get_queried_object_id();
310 $template = get_page_template_slug();
311 $pagename = get_query_var('pagename');
313 if ( ! $pagename && $id ) {
314 // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
315 $post = get_queried_object();
317 $pagename = $post->post_name;
320 $templates = array();
321 if ( $template && 0 === validate_file( $template ) )
322 $templates[] = $template;
324 $templates[] = "page-$pagename.php";
326 $templates[] = "page-$id.php";
327 $templates[] = 'page.php';
329 return get_query_template( 'page', $templates );
333 * Retrieve path of paged template in current or parent template.
335 * The template path is filterable via the 'paged_template' hook.
339 * @see get_query_template()
341 * @return string Full path to paged template file.
343 function get_paged_template() {
344 return get_query_template('paged');
348 * Retrieve path of search template in current or parent template.
350 * The template path is filterable via the 'search_template' hook.
354 * @see get_query_template()
356 * @return string Full path to search template file.
358 function get_search_template() {
359 return get_query_template('search');
363 * Retrieve path of single template in current or parent template.
365 * The template path is filterable via the 'single_template' hook.
369 * @see get_query_template()
371 * @return string Full path to single template file.
373 function get_single_template() {
374 $object = get_queried_object();
376 $templates = array();
378 if ( ! empty( $object->post_type ) )
379 $templates[] = "single-{$object->post_type}.php";
380 $templates[] = "single.php";
382 return get_query_template( 'single', $templates );
386 * Retrieve path of attachment template in current or parent template.
388 * The attachment path first checks if the first part of the mime type exists.
389 * The second check is for the second part of the mime type. The last check is
390 * for both types separated by an underscore. If neither are found then the file
391 * 'attachment.php' is checked and returned.
393 * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
394 * finally 'text_plain.php'.
396 * The template path is filterable via the 'attachment_template' hook.
400 * @see get_query_template()
402 * @return string Full path to attachment template file.
404 function get_attachment_template() {
407 if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) ) {
408 $type = explode( '/', $posts[0]->post_mime_type );
410 if ( ! empty( $type ) ) {
411 if ( $template = get_query_template( $type[0] ) )
413 elseif ( ! empty( $type[1] ) ) {
414 if ( $template = get_query_template( $type[1] ) )
416 elseif ( $template = get_query_template( "$type[0]_$type[1]" ) )
422 return get_query_template( 'attachment' );
426 * Retrieve path of comment popup template in current or parent template.
428 * Checks for comment popup template in current template, if it exists or in the
431 * The template path is filterable via the 'comments_popup_template' hook.
435 * @see get_query_template()
437 * @return string Full path to comments popup template file.
439 function get_comments_popup_template() {
440 $template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );
442 // Backward compat code will be removed in a future release
444 $template = ABSPATH . WPINC . '/theme-compat/comments-popup.php';
450 * Retrieve the name of the highest priority template file that exists.
452 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
453 * inherit from a parent theme can just overload one file.
457 * @param string|array $template_names Template file(s) to search for, in order.
458 * @param bool $load If true the template file will be loaded if it is found.
459 * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
460 * @return string The template filename if one is located.
462 function locate_template($template_names, $load = false, $require_once = true ) {
464 foreach ( (array) $template_names as $template_name ) {
465 if ( !$template_name )
467 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
468 $located = STYLESHEETPATH . '/' . $template_name;
470 } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
471 $located = TEMPLATEPATH . '/' . $template_name;
476 if ( $load && '' != $located )
477 load_template( $located, $require_once );
483 * Require the template file with WordPress environment.
485 * The globals are set up for the template file to ensure that the WordPress
486 * environment is available from within the function. The query variables are
491 * @param string $_template_file Path to template file.
492 * @param bool $require_once Whether to require_once or require. Default true.
494 function load_template( $_template_file, $require_once = true ) {
495 global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
497 if ( is_array( $wp_query->query_vars ) )
498 extract( $wp_query->query_vars, EXTR_SKIP );
501 require_once( $_template_file );
503 require( $_template_file );