wp/wp-includes/template.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     9 /**
     9 /**
    10  * Retrieve path to a template
    10  * Retrieve path to a template
    11  *
    11  *
    12  * Used to quickly retrieve the path of a template without including the file
    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
    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
    14  * the use of locate_template(). Allows for more generic template location
    15  * without the use of the other get_*_template() functions.
    15  * without the use of the other get_*_template() functions.
    16  *
    16  *
    17  * @since 1.5.0
    17  * @since 1.5.0
    18  *
    18  *
    19  * @param string $type Filename without extension.
    19  * @param string $type      Filename without extension.
    20  * @param array $templates An optional list of template candidates
    20  * @param array  $templates An optional list of template candidates
    21  * @return string Full path to template file.
    21  * @return string Full path to template file.
    22  */
    22  */
    23 function get_query_template( $type, $templates = array() ) {
    23 function get_query_template( $type, $templates = array() ) {
    24 	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
    24 	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
    25 
    25 
    26 	if ( empty( $templates ) )
    26 	if ( empty( $templates ) )
    27 		$templates = array("{$type}.php");
    27 		$templates = array("{$type}.php");
    28 
    28 
       
    29 	/**
       
    30 	 * Filters the list of template filenames that are searched for when retrieving a template to use.
       
    31 	 *
       
    32 	 * The last element in the array should always be the fallback template for this query type.
       
    33 	 *
       
    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'.
       
    36 	 *
       
    37 	 * @since 4.7.0
       
    38 	 *
       
    39 	 * @param array $templates A list of template candidates, in descending order of priority.
       
    40 	 */
       
    41 	$templates = apply_filters( "{$type}_template_hierarchy", $templates );
       
    42 
    29 	$template = locate_template( $templates );
    43 	$template = locate_template( $templates );
       
    44 
    30 	/**
    45 	/**
    31 	 * Filter the path of the queried template by type.
    46 	 * Filters the path of the queried template by type.
    32 	 *
    47 	 *
    33 	 * The dynamic portion of the hook name, `$type`, refers to the filename
    48 	 * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
    34 	 * -- minus the extension -- of the file to load. This hook also applies
    49 	 * extension and any non-alphanumeric characters delimiting words -- of the file to load.
    35 	 * to various types of files loaded as part of the Template Hierarchy.
    50 	 * This hook also applies to various types of files loaded as part of the Template Hierarchy.
       
    51 	 *
       
    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'.
    36 	 *
    54 	 *
    37 	 * @since 1.5.0
    55 	 * @since 1.5.0
    38 	 *
    56 	 * @since 4.8.0 The `$type` and `$templates` parameters were added.
    39 	 * @param string $template Path to the template. See {@see locate_template()}.
    57 	 *
       
    58 	 * @param string $template  Path to the template. See locate_template().
       
    59 	 * @param string $type      Filename without extension.
       
    60 	 * @param array  $templates A list of template candidates, in descending order of priority.
    40 	 */
    61 	 */
    41 	return apply_filters( "{$type}_template", $template );
    62 	return apply_filters( "{$type}_template", $template, $type, $templates );
    42 }
    63 }
    43 
    64 
    44 /**
    65 /**
    45  * Retrieve path of index template in current or parent template.
    66  * Retrieve path of index template in current or parent template.
    46  *
    67  *
    47  * The template path is filterable via the 'index_template' hook.
    68  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
    69  * and {@see '$type_template'} dynamic hooks, where `$type` is 'index'.
    48  *
    70  *
    49  * @since 3.0.0
    71  * @since 3.0.0
    50  *
    72  *
    51  * @see get_query_template()
    73  * @see get_query_template()
    52  *
    74  *
    57 }
    79 }
    58 
    80 
    59 /**
    81 /**
    60  * Retrieve path of 404 template in current or parent template.
    82  * Retrieve path of 404 template in current or parent template.
    61  *
    83  *
    62  * The template path is filterable via the '404_template' hook.
    84  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
    85  * and {@see '$type_template'} dynamic hooks, where `$type` is '404'.
    63  *
    86  *
    64  * @since 1.5.0
    87  * @since 1.5.0
    65  *
    88  *
    66  * @see get_query_template()
    89  * @see get_query_template()
    67  *
    90  *
    72 }
    95 }
    73 
    96 
    74 /**
    97 /**
    75  * Retrieve path of archive template in current or parent template.
    98  * Retrieve path of archive template in current or parent template.
    76  *
    99  *
    77  * The template path is filterable via the 'archive_template' hook.
   100  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   101  * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
    78  *
   102  *
    79  * @since 1.5.0
   103  * @since 1.5.0
    80  *
   104  *
    81  * @see get_query_template()
   105  * @see get_query_template()
    82  *
   106  *
    97 }
   121 }
    98 
   122 
    99 /**
   123 /**
   100  * Retrieve path of post type archive template in current or parent template.
   124  * Retrieve path of post type archive template in current or parent template.
   101  *
   125  *
   102  * The template path is filterable via the 'archive_template' hook.
   126  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   127  * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
   103  *
   128  *
   104  * @since 3.7.0
   129  * @since 3.7.0
   105  *
   130  *
   106  * @see get_archive_template()
   131  * @see get_archive_template()
   107  *
   132  *
   111 	$post_type = get_query_var( 'post_type' );
   136 	$post_type = get_query_var( 'post_type' );
   112 	if ( is_array( $post_type ) )
   137 	if ( is_array( $post_type ) )
   113 		$post_type = reset( $post_type );
   138 		$post_type = reset( $post_type );
   114 
   139 
   115 	$obj = get_post_type_object( $post_type );
   140 	$obj = get_post_type_object( $post_type );
   116 	if ( ! $obj->has_archive )
   141 	if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
   117 		return '';
   142 		return '';
       
   143 	}
   118 
   144 
   119 	return get_archive_template();
   145 	return get_archive_template();
   120 }
   146 }
   121 
   147 
   122 /**
   148 /**
   123  * Retrieve path of author template in current or parent template.
   149  * Retrieve path of author template in current or parent template.
   124  *
   150  *
   125  * The template path is filterable via the 'author_template' hook.
   151  * The hierarchy for this template looks like:
       
   152  *
       
   153  * 1. author-{nicename}.php
       
   154  * 2. author-{id}.php
       
   155  * 3. author.php
       
   156  *
       
   157  * An example of this is:
       
   158  *
       
   159  * 1. author-john.php
       
   160  * 2. author-1.php
       
   161  * 3. author.php
       
   162  *
       
   163  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   164  * and {@see '$type_template'} dynamic hooks, where `$type` is 'author'.
   126  *
   165  *
   127  * @since 1.5.0
   166  * @since 1.5.0
   128  *
   167  *
   129  * @see get_query_template()
   168  * @see get_query_template()
   130  *
   169  *
   145 }
   184 }
   146 
   185 
   147 /**
   186 /**
   148  * Retrieve path of category template in current or parent template.
   187  * Retrieve path of category template in current or parent template.
   149  *
   188  *
   150  * Works by first retrieving the current slug, for example 'category-default.php', and then
   189  * The hierarchy for this template looks like:
   151  * trying category ID, for example 'category-1.php', and will finally fall back to category.php
   190  *
   152  * template, if those files don't exist.
   191  * 1. category-{slug}.php
   153  *
   192  * 2. category-{id}.php
   154  * The template path is filterable via the 'category_template' hook.
   193  * 3. category.php
   155  *
   194  *
   156  * @since 1.5.0
   195  * An example of this is:
       
   196  *
       
   197  * 1. category-news.php
       
   198  * 2. category-2.php
       
   199  * 3. category.php
       
   200  *
       
   201  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   202  * and {@see '$type_template'} dynamic hooks, where `$type` is 'category'.
       
   203  *
       
   204  * @since 1.5.0
       
   205  * @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
       
   206  *              template hierarchy when the category slug contains multibyte characters.
   157  *
   207  *
   158  * @see get_query_template()
   208  * @see get_query_template()
   159  *
   209  *
   160  * @return string Full path to category template file.
   210  * @return string Full path to category template file.
   161  */
   211  */
   163 	$category = get_queried_object();
   213 	$category = get_queried_object();
   164 
   214 
   165 	$templates = array();
   215 	$templates = array();
   166 
   216 
   167 	if ( ! empty( $category->slug ) ) {
   217 	if ( ! empty( $category->slug ) ) {
       
   218 
       
   219 		$slug_decoded = urldecode( $category->slug );
       
   220 		if ( $slug_decoded !== $category->slug ) {
       
   221 			$templates[] = "category-{$slug_decoded}.php";
       
   222 		}
       
   223 
   168 		$templates[] = "category-{$category->slug}.php";
   224 		$templates[] = "category-{$category->slug}.php";
   169 		$templates[] = "category-{$category->term_id}.php";
   225 		$templates[] = "category-{$category->term_id}.php";
   170 	}
   226 	}
   171 	$templates[] = 'category.php';
   227 	$templates[] = 'category.php';
   172 
   228 
   174 }
   230 }
   175 
   231 
   176 /**
   232 /**
   177  * Retrieve path of tag template in current or parent template.
   233  * Retrieve path of tag template in current or parent template.
   178  *
   234  *
   179  * Works by first retrieving the current tag name, for example 'tag-wordpress.php', and then
   235  * The hierarchy for this template looks like:
   180  * trying tag ID, for example 'tag-1.php', and will finally fall back to tag.php
   236  *
   181  * template, if those files don't exist.
   237  * 1. tag-{slug}.php
   182  *
   238  * 2. tag-{id}.php
   183  * The template path is filterable via the 'tag_template' hook.
   239  * 3. tag.php
       
   240  *
       
   241  * An example of this is:
       
   242  *
       
   243  * 1. tag-wordpress.php
       
   244  * 2. tag-3.php
       
   245  * 3. tag.php
       
   246  *
       
   247  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   248  * and {@see '$type_template'} dynamic hooks, where `$type` is 'tag'.
   184  *
   249  *
   185  * @since 2.3.0
   250  * @since 2.3.0
       
   251  * @since 4.7.0 The decoded form of `tag-{slug}.php` was added to the top of the
       
   252  *              template hierarchy when the tag slug contains multibyte characters.
   186  *
   253  *
   187  * @see get_query_template()
   254  * @see get_query_template()
   188  *
   255  *
   189  * @return string Full path to tag template file.
   256  * @return string Full path to tag template file.
   190  */
   257  */
   192 	$tag = get_queried_object();
   259 	$tag = get_queried_object();
   193 
   260 
   194 	$templates = array();
   261 	$templates = array();
   195 
   262 
   196 	if ( ! empty( $tag->slug ) ) {
   263 	if ( ! empty( $tag->slug ) ) {
       
   264 
       
   265 		$slug_decoded = urldecode( $tag->slug );
       
   266 		if ( $slug_decoded !== $tag->slug ) {
       
   267 			$templates[] = "tag-{$slug_decoded}.php";
       
   268 		}
       
   269 
   197 		$templates[] = "tag-{$tag->slug}.php";
   270 		$templates[] = "tag-{$tag->slug}.php";
   198 		$templates[] = "tag-{$tag->term_id}.php";
   271 		$templates[] = "tag-{$tag->term_id}.php";
   199 	}
   272 	}
   200 	$templates[] = 'tag.php';
   273 	$templates[] = 'tag.php';
   201 
   274 
   202 	return get_query_template( 'tag', $templates );
   275 	return get_query_template( 'tag', $templates );
   203 }
   276 }
   204 
   277 
   205 /**
   278 /**
   206  * Retrieve path of taxonomy template in current or parent template.
   279  * Retrieve path of custom taxonomy term template in current or parent template.
   207  *
   280  *
   208  * Retrieves the taxonomy and term, if term is available. The template is
   281  * The hierarchy for this template looks like:
   209  * prepended with 'taxonomy-' and followed by both the taxonomy string and
   282  *
   210  * the taxonomy string followed by a dash and then followed by the term.
   283  * 1. taxonomy-{taxonomy_slug}-{term_slug}.php
   211  *
   284  * 2. taxonomy-{taxonomy_slug}.php
   212  * The taxonomy and term template is checked and used first, if it exists.
   285  * 3. taxonomy.php
   213  * Second, just the taxonomy template is checked, and then finally, taxonomy.php
   286  *
   214  * template is used. If none of the files exist, then it will fall back on to
   287  * An example of this is:
   215  * index.php.
   288  *
   216  *
   289  * 1. taxonomy-location-texas.php
   217  * The template path is filterable via the 'taxonomy_template' hook.
   290  * 2. taxonomy-location.php
       
   291  * 3. taxonomy.php
       
   292  *
       
   293  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   294  * and {@see '$type_template'} dynamic hooks, where `$type` is 'taxonomy'.
   218  *
   295  *
   219  * @since 2.5.0
   296  * @since 2.5.0
   220  *
   297  * @since 4.7.0 The decoded form of `taxonomy-{taxonomy_slug}-{term_slug}.php` was added to the top of the
   221  * @see get_query_template()
   298  *              template hierarchy when the term slug contains multibyte characters.
   222  *
   299  *
   223  * @return string Full path to taxonomy template file.
   300  * @see get_query_template()
       
   301  *
       
   302  * @return string Full path to custom taxonomy term template file.
   224  */
   303  */
   225 function get_taxonomy_template() {
   304 function get_taxonomy_template() {
   226 	$term = get_queried_object();
   305 	$term = get_queried_object();
   227 
   306 
   228 	$templates = array();
   307 	$templates = array();
   229 
   308 
   230 	if ( ! empty( $term->slug ) ) {
   309 	if ( ! empty( $term->slug ) ) {
   231 		$taxonomy = $term->taxonomy;
   310 		$taxonomy = $term->taxonomy;
       
   311 
       
   312 		$slug_decoded = urldecode( $term->slug );
       
   313 		if ( $slug_decoded !== $term->slug ) {
       
   314 			$templates[] = "taxonomy-$taxonomy-{$slug_decoded}.php";
       
   315 		}
       
   316 
   232 		$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
   317 		$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
   233 		$templates[] = "taxonomy-$taxonomy.php";
   318 		$templates[] = "taxonomy-$taxonomy.php";
   234 	}
   319 	}
   235 	$templates[] = 'taxonomy.php';
   320 	$templates[] = 'taxonomy.php';
   236 
   321 
   238 }
   323 }
   239 
   324 
   240 /**
   325 /**
   241  * Retrieve path of date template in current or parent template.
   326  * Retrieve path of date template in current or parent template.
   242  *
   327  *
   243  * The template path is filterable via the 'date_template' hook.
   328  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   329  * and {@see '$type_template'} dynamic hooks, where `$type` is 'date'.
   244  *
   330  *
   245  * @since 1.5.0
   331  * @since 1.5.0
   246  *
   332  *
   247  * @see get_query_template()
   333  * @see get_query_template()
   248  *
   334  *
   253 }
   339 }
   254 
   340 
   255 /**
   341 /**
   256  * Retrieve path of home template in current or parent template.
   342  * Retrieve path of home template in current or parent template.
   257  *
   343  *
   258  * This is the template used for the page containing the blog posts.
   344  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   259  * Attempts to locate 'home.php' first before falling back to 'index.php'.
   345  * and {@see '$type_template'} dynamic hooks, where `$type` is 'home'.
   260  *
       
   261  * The template path is filterable via the 'home_template' hook.
       
   262  *
   346  *
   263  * @since 1.5.0
   347  * @since 1.5.0
   264  *
   348  *
   265  * @see get_query_template()
   349  * @see get_query_template()
   266  *
   350  *
   271 
   355 
   272 	return get_query_template( 'home', $templates );
   356 	return get_query_template( 'home', $templates );
   273 }
   357 }
   274 
   358 
   275 /**
   359 /**
   276  * Retrieve path of front-page template in current or parent template.
   360  * Retrieve path of front page template in current or parent template.
   277  *
   361  *
   278  * Looks for 'front-page.php'. The template path is filterable via the
   362  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   279  * 'front_page_template' hook.
   363  * and {@see '$type_template'} dynamic hooks, where `$type` is 'frontpage'.
   280  *
   364  *
   281  * @since 3.0.0
   365  * @since 3.0.0
   282  *
   366  *
   283  * @see get_query_template()
   367  * @see get_query_template()
   284  *
   368  *
   291 }
   375 }
   292 
   376 
   293 /**
   377 /**
   294  * Retrieve path of page template in current or parent template.
   378  * Retrieve path of page template in current or parent template.
   295  *
   379  *
   296  * Will first look for the specifically assigned page template.
   380  * The hierarchy for this template looks like:
   297  * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
   381  *
   298  * and finally 'page.php'.
   382  * 1. {Page Template}.php
   299  *
   383  * 2. page-{page_name}.php
   300  * The template path is filterable via the 'page_template' hook.
   384  * 3. page-{id}.php
   301  *
   385  * 4. page.php
   302  * @since 1.5.0
   386  *
       
   387  * An example of this is:
       
   388  *
       
   389  * 1. page-templates/full-width.php
       
   390  * 2. page-about.php
       
   391  * 3. page-4.php
       
   392  * 4. page.php
       
   393  *
       
   394  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   395  * and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
       
   396  *
       
   397  * @since 1.5.0
       
   398  * @since 4.7.0 The decoded form of `page-{page_name}.php` was added to the top of the
       
   399  *              template hierarchy when the page name contains multibyte characters.
   303  *
   400  *
   304  * @see get_query_template()
   401  * @see get_query_template()
   305  *
   402  *
   306  * @return string Full path to page template file.
   403  * @return string Full path to page template file.
   307  */
   404  */
   318 	}
   415 	}
   319 
   416 
   320 	$templates = array();
   417 	$templates = array();
   321 	if ( $template && 0 === validate_file( $template ) )
   418 	if ( $template && 0 === validate_file( $template ) )
   322 		$templates[] = $template;
   419 		$templates[] = $template;
   323 	if ( $pagename )
   420 	if ( $pagename ) {
   324 		$templates[] = "page-$pagename.php";
   421 		$pagename_decoded = urldecode( $pagename );
       
   422 		if ( $pagename_decoded !== $pagename ) {
       
   423 			$templates[] = "page-{$pagename_decoded}.php";
       
   424 		}
       
   425 		$templates[] = "page-{$pagename}.php";
       
   426 	}
   325 	if ( $id )
   427 	if ( $id )
   326 		$templates[] = "page-$id.php";
   428 		$templates[] = "page-{$id}.php";
   327 	$templates[] = 'page.php';
   429 	$templates[] = 'page.php';
   328 
   430 
   329 	return get_query_template( 'page', $templates );
   431 	return get_query_template( 'page', $templates );
   330 }
   432 }
   331 
   433 
   332 /**
   434 /**
   333  * Retrieve path of paged template in current or parent template.
       
   334  *
       
   335  * The template path is filterable via the 'paged_template' hook.
       
   336  *
       
   337  * @since 1.5.0
       
   338  *
       
   339  * @see get_query_template()
       
   340  *
       
   341  * @return string Full path to paged template file.
       
   342  */
       
   343 function get_paged_template() {
       
   344 	return get_query_template('paged');
       
   345 }
       
   346 
       
   347 /**
       
   348  * Retrieve path of search template in current or parent template.
   435  * Retrieve path of search template in current or parent template.
   349  *
   436  *
   350  * The template path is filterable via the 'search_template' hook.
   437  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   438  * and {@see '$type_template'} dynamic hooks, where `$type` is 'search'.
   351  *
   439  *
   352  * @since 1.5.0
   440  * @since 1.5.0
   353  *
   441  *
   354  * @see get_query_template()
   442  * @see get_query_template()
   355  *
   443  *
   358 function get_search_template() {
   446 function get_search_template() {
   359 	return get_query_template('search');
   447 	return get_query_template('search');
   360 }
   448 }
   361 
   449 
   362 /**
   450 /**
   363  * Retrieve path of single template in current or parent template.
   451  * Retrieve path of single template in current or parent template. Applies to single Posts,
   364  *
   452  * single Attachments, and single custom post types.
   365  * The template path is filterable via the 'single_template' hook.
   453  *
   366  *
   454  * The hierarchy for this template looks like:
   367  * @since 1.5.0
   455  *
       
   456  * 1. {Post Type Template}.php
       
   457  * 2. single-{post_type}-{post_name}.php
       
   458  * 3. single-{post_type}.php
       
   459  * 4. single.php
       
   460  *
       
   461  * An example of this is:
       
   462  *
       
   463  * 1. templates/full-width.php
       
   464  * 2. single-post-hello-world.php
       
   465  * 3. single-post.php
       
   466  * 4. single.php
       
   467  *
       
   468  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   469  * and {@see '$type_template'} dynamic hooks, where `$type` is 'single'.
       
   470  *
       
   471  * @since 1.5.0
       
   472  * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
       
   473  * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
       
   474  *              template hierarchy when the post name contains multibyte characters.
       
   475  * @since 4.7.0 {Post Type Template}.php was added to the top of the template hierarchy.
   368  *
   476  *
   369  * @see get_query_template()
   477  * @see get_query_template()
   370  *
   478  *
   371  * @return string Full path to single template file.
   479  * @return string Full path to single template file.
   372  */
   480  */
   373 function get_single_template() {
   481 function get_single_template() {
   374 	$object = get_queried_object();
   482 	$object = get_queried_object();
   375 
   483 
   376 	$templates = array();
   484 	$templates = array();
   377 
   485 
   378 	if ( ! empty( $object->post_type ) )
   486 	if ( ! empty( $object->post_type ) ) {
       
   487 		$template = get_page_template_slug( $object );
       
   488 		if ( $template && 0 === validate_file( $template ) ) {
       
   489 			$templates[] = $template;
       
   490 		}
       
   491 
       
   492 		$name_decoded = urldecode( $object->post_name );
       
   493 		if ( $name_decoded !== $object->post_name ) {
       
   494 			$templates[] = "single-{$object->post_type}-{$name_decoded}.php";
       
   495 		}
       
   496 
       
   497 		$templates[] = "single-{$object->post_type}-{$object->post_name}.php";
   379 		$templates[] = "single-{$object->post_type}.php";
   498 		$templates[] = "single-{$object->post_type}.php";
       
   499 	}
       
   500 
   380 	$templates[] = "single.php";
   501 	$templates[] = "single.php";
   381 
   502 
   382 	return get_query_template( 'single', $templates );
   503 	return get_query_template( 'single', $templates );
   383 }
   504 }
   384 
   505 
   385 /**
   506 /**
       
   507  * Retrieves an embed template path in the current or parent template.
       
   508  *
       
   509  * The hierarchy for this template looks like:
       
   510  *
       
   511  * 1. embed-{post_type}-{post_format}.php
       
   512  * 2. embed-{post_type}.php
       
   513  * 3. embed.php
       
   514  *
       
   515  * An example of this is:
       
   516  *
       
   517  * 1. embed-post-audio.php
       
   518  * 2. embed-post.php
       
   519  * 3. embed.php
       
   520  *
       
   521  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   522  * and {@see '$type_template'} dynamic hooks, where `$type` is 'embed'.
       
   523  *
       
   524  * @since 4.5.0
       
   525  *
       
   526  * @see get_query_template()
       
   527  *
       
   528  * @return string Full path to embed template file.
       
   529  */
       
   530 function get_embed_template() {
       
   531 	$object = get_queried_object();
       
   532 
       
   533 	$templates = array();
       
   534 
       
   535 	if ( ! empty( $object->post_type ) ) {
       
   536 		$post_format = get_post_format( $object );
       
   537 		if ( $post_format ) {
       
   538 			$templates[] = "embed-{$object->post_type}-{$post_format}.php";
       
   539 		}
       
   540 		$templates[] = "embed-{$object->post_type}.php";
       
   541 	}
       
   542 
       
   543 	$templates[] = "embed.php";
       
   544 
       
   545 	return get_query_template( 'embed', $templates );
       
   546 }
       
   547 
       
   548 /**
       
   549  * Retrieves the path of the singular template in current or parent template.
       
   550  *
       
   551  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   552  * and {@see '$type_template'} dynamic hooks, where `$type` is 'singular'.
       
   553  *
       
   554  * @since 4.3.0
       
   555  *
       
   556  * @see get_query_template()
       
   557  *
       
   558  * @return string Full path to singular template file
       
   559  */
       
   560 function get_singular_template() {
       
   561 	return get_query_template( 'singular' );
       
   562 }
       
   563 
       
   564 /**
   386  * Retrieve path of attachment template in current or parent template.
   565  * Retrieve path of attachment template in current or parent template.
   387  *
   566  *
   388  * The attachment path first checks if the first part of the mime type exists.
   567  * The hierarchy for this template looks like:
   389  * The second check is for the second part of the mime type. The last check is
   568  *
   390  * for both types separated by an underscore. If neither are found then the file
   569  * 1. {mime_type}-{sub_type}.php
   391  * 'attachment.php' is checked and returned.
   570  * 2. {sub_type}.php
   392  *
   571  * 3. {mime_type}.php
   393  * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
   572  * 4. attachment.php
   394  * finally 'text_plain.php'.
   573  *
   395  *
   574  * An example of this is:
   396  * The template path is filterable via the 'attachment_template' hook.
   575  *
       
   576  * 1. image-jpeg.php
       
   577  * 2. jpeg.php
       
   578  * 3. image.php
       
   579  * 4. attachment.php
       
   580  *
       
   581  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   582  * and {@see '$type_template'} dynamic hooks, where `$type` is 'attachment'.
   397  *
   583  *
   398  * @since 2.0.0
   584  * @since 2.0.0
   399  *
   585  * @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
   400  * @see get_query_template()
   586  *
       
   587  * @see get_query_template()
       
   588  *
       
   589  * @global array $posts
   401  *
   590  *
   402  * @return string Full path to attachment template file.
   591  * @return string Full path to attachment template file.
   403  */
   592  */
   404 function get_attachment_template() {
   593 function get_attachment_template() {
   405 	global $posts;
   594 	$attachment = get_queried_object();
   406 
   595 
   407 	if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) ) {
   596 	$templates = array();
   408 		$type = explode( '/', $posts[0]->post_mime_type );
   597 
   409 
   598 	if ( $attachment ) {
   410 		if ( ! empty( $type ) ) {
   599 		if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
   411 			if ( $template = get_query_template( $type[0] ) )
   600 			list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
   412 				return $template;
   601 		} else {
   413 			elseif ( ! empty( $type[1] ) ) {
   602 			list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
   414 				if ( $template = get_query_template( $type[1] ) )
   603 		}
   415 					return $template;
   604 
   416 				elseif ( $template = get_query_template( "$type[0]_$type[1]" ) )
   605 		if ( ! empty( $subtype ) ) {
   417 					return $template;
   606 			$templates[] = "{$type}-{$subtype}.php";
   418 			}
   607 			$templates[] = "{$subtype}.php";
   419 		}
   608 		}
   420 	}
   609 		$templates[] = "{$type}.php";
   421 
   610 	}
   422 	return get_query_template( 'attachment' );
   611 	$templates[] = 'attachment.php';
   423 }
   612 
   424 
   613 	return get_query_template( 'attachment', $templates );
   425 /**
       
   426  * Retrieve path of comment popup template in current or parent template.
       
   427  *
       
   428  * Checks for comment popup template in current template, if it exists or in the
       
   429  * parent template.
       
   430  *
       
   431  * The template path is filterable via the 'comments_popup_template' hook.
       
   432  *
       
   433  * @since 1.5.0
       
   434  *
       
   435  * @see get_query_template()
       
   436  *
       
   437  * @return string Full path to comments popup template file.
       
   438  */
       
   439 function get_comments_popup_template() {
       
   440 	$template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );
       
   441 
       
   442 	// Backward compat code will be removed in a future release
       
   443 	if ('' == $template)
       
   444 		$template = ABSPATH . WPINC . '/theme-compat/comments-popup.php';
       
   445 
       
   446 	return $template;
       
   447 }
   614 }
   448 
   615 
   449 /**
   616 /**
   450  * Retrieve the name of the highest priority template file that exists.
   617  * Retrieve the name of the highest priority template file that exists.
   451  *
   618  *
   452  * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
   619  * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
   453  * inherit from a parent theme can just overload one file.
   620  * so that themes which inherit from a parent theme can just overload one file.
   454  *
   621  *
   455  * @since 2.7.0
   622  * @since 2.7.0
   456  *
   623  *
   457  * @param string|array $template_names Template file(s) to search for, in order.
   624  * @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.
   625  * @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.
   626  * @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.
   627  * @return string The template filename if one is located.
   461  */
   628  */
   462 function locate_template($template_names, $load = false, $require_once = true ) {
   629 function locate_template($template_names, $load = false, $require_once = true ) {
   463 	$located = '';
   630 	$located = '';
   464 	foreach ( (array) $template_names as $template_name ) {
   631 	foreach ( (array) $template_names as $template_name ) {
   468 			$located = STYLESHEETPATH . '/' . $template_name;
   635 			$located = STYLESHEETPATH . '/' . $template_name;
   469 			break;
   636 			break;
   470 		} elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
   637 		} elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
   471 			$located = TEMPLATEPATH . '/' . $template_name;
   638 			$located = TEMPLATEPATH . '/' . $template_name;
   472 			break;
   639 			break;
       
   640 		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
       
   641 			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
       
   642 			break;
   473 		}
   643 		}
   474 	}
   644 	}
   475 
   645 
   476 	if ( $load && '' != $located )
   646 	if ( $load && '' != $located )
   477 		load_template( $located, $require_once );
   647 		load_template( $located, $require_once );
   486  * environment is available from within the function. The query variables are
   656  * environment is available from within the function. The query variables are
   487  * also available.
   657  * also available.
   488  *
   658  *
   489  * @since 1.5.0
   659  * @since 1.5.0
   490  *
   660  *
       
   661  * @global array      $posts
       
   662  * @global WP_Post    $post
       
   663  * @global bool       $wp_did_header
       
   664  * @global WP_Query   $wp_query
       
   665  * @global WP_Rewrite $wp_rewrite
       
   666  * @global wpdb       $wpdb
       
   667  * @global string     $wp_version
       
   668  * @global WP         $wp
       
   669  * @global int        $id
       
   670  * @global WP_Comment $comment
       
   671  * @global int        $user_ID
       
   672  *
   491  * @param string $_template_file Path to template file.
   673  * @param string $_template_file Path to template file.
   492  * @param bool $require_once Whether to require_once or require. Default true.
   674  * @param bool   $require_once   Whether to require_once or require. Default true.
   493  */
   675  */
   494 function load_template( $_template_file, $require_once = true ) {
   676 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;
   677 	global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
   496 
   678 
   497 	if ( is_array( $wp_query->query_vars ) )
   679 	if ( is_array( $wp_query->query_vars ) ) {
   498 		extract( $wp_query->query_vars, EXTR_SKIP );
   680 		extract( $wp_query->query_vars, EXTR_SKIP );
   499 
   681 	}
   500 	if ( $require_once )
   682 
       
   683 	if ( isset( $s ) ) {
       
   684 		$s = esc_attr( $s );
       
   685 	}
       
   686 
       
   687 	if ( $require_once ) {
   501 		require_once( $_template_file );
   688 		require_once( $_template_file );
   502 	else
   689 	} else {
   503 		require( $_template_file );
   690 		require( $_template_file );
   504 }
   691 	}
   505 
   692 }