返回介绍

get_query_template()

发布于 2017-09-11 00:03:11 字数 5241 浏览 994 评论 0 收藏 0

get_query_template( string $type,  array $templates = array() )

Retrieve path to a template


description

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 candidates

Default 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 );
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

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

User Contributed Notes

  1. 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() );
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文