get_query_template( string $type, array $templates = array() )
Retrieve path to a template
描述
Used to quickly retrieve the path of a template without including the file extension. It will also check the parent theme, if the file exists, with the use of locate_template(). Allows for more generic template location without the use of the other get_*_template() functions.
参数
- $type
-
(string)
(Required)
Filename without extension. - $templates
-
(array)
(Optional)
An optional list of template candidatesDefault value: array()
返回值
(string) Full path to template file.
源代码
File: wp-includes/template.php
function get_query_template( $type, $templates = array() ) {
$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
if ( empty( $templates ) )
$templates = array("{$type}.php");
/**
* Filters the list of template filenames that are searched for when retrieving a template to use.
*
* The last element in the array should always be the fallback template for this query type.
*
* Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
* 'embed', home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
*
* @since 4.7.0
*
* @param array $templates A list of template candidates, in descending order of priority.
*/
$templates = apply_filters( "{$type}_template_hierarchy", $templates );
$template = locate_template( $templates );
/**
* Filters the path of the queried template by type.
*
* The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
* extension and any non-alphanumeric characters delimiting words -- of the file to load.
* This hook also applies to various types of files loaded as part of the Template Hierarchy.
*
* Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
* 'embed', home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
*
* @since 1.5.0
* @since 4.8.0 The `$type` and `$templates` parameters were added.
*
* @param string $template Path to the template. See locate_template().
* @param string $type Filename without extension.
* @param array $templates A list of template candidates, in descending order of priority.
*/
return apply_filters( "{$type}_template", $template, $type, $templates );
}
更新日志
Version | 描述 |
---|---|
1.5.0 | Introduced. |
相关函数
Uses
-
wp-includes/template.php:
{$type}_template_hierarchy -
wp-includes/plugin.php:
apply_filters() -
wp-includes/template.php:
locate_template() -
wp-includes/template.php:
{$type}_template
Used By
-
wp-includes/template.php:
get_embed_template() -
wp-includes/template.php:
get_singular_template() -
wp-includes/template.php:
get_search_template() -
wp-includes/template.php:
get_single_template() -
wp-includes/template.php:
get_attachment_template() -
wp-includes/template.php:
get_index_template() -
wp-includes/template.php:
get_404_template() -
wp-includes/template.php:
get_archive_template() -
wp-includes/template.php:
get_author_template() -
wp-includes/template.php:
get_category_template() -
wp-includes/template.php:
get_tag_template() -
wp-includes/template.php:
get_taxonomy_template() -
wp-includes/template.php:
get_date_template() -
wp-includes/template.php:
get_home_template() -
wp-includes/template.php:
get_front_page_template() -
wp-includes/template.php:
get_page_template() -
wp-includes/deprecated.php:
get_paged_template()
Show 12 more used by
Hide more used by
-
Skip to note content
You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note
Contributed by
Codex
Example
Can be used with include() or require() to retrieve path.
if ( '' !== get_query_template( '404' ) ) { include( get_query_template( '404' ) ); }
or the same can be accomplished with
if ( '' != get_404_template() ) { include( get_404_template() ); }
你可能对这些文章感兴趣:
- wordpress函数get_the_posts_navigation()用法示例
- wordpress函数get_the_title()用法示例
- wordpress函数get_the_terms()用法示例
- wordpress函数get_the_tags()用法示例
- wordpress函数get_the_taxonomies()用法示例
- wordpress函数get_the_permalink()用法示例
- wordpress函数get_the_password_form()用法示例
- wordpress函数get_the_posts_pagination()用法示例
- wordpress函数get_the_modified_date()用法示例
- wordpress函数get_the_modified_time()用法示例
- wordpress函数get_the_modified_author()用法示例
- wordpress函数get_the_guid()用法示例
- wordpress函数get_the_ID()用法示例
- wordpress函数get_the_excerpt()用法示例
如有疑问,请前往问答中心反馈!
反馈