WordPress函数get_query_template()

描述:

不用扩展名检索文件路径。

该函数可不检查文件扩展名而快速检索文件路径。若文件存在,函数也可以用locate_template()查看父模板。如果不使用其他get_*_template()函数,允许检索结果出现通用文件所在位置。

该函数可与 include()或require() 共同检索路径:

if (!= get_query_template( '404' ) );
include( get_query_template( '404' ) );

以下用法也可实现同样的效果:

if (  != get_404_template() );
include( get_404_template() );

用法:

<?php get_query_template( $type, $templates ); ?>

参数:

$type

(string) (必填) 不带扩展名的文件名。

默认值: None

$templates

(array) (可选) 候选模板的可选列表

默认值: 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 {@link locate_template()}. Allows for more generic template location
 * without the use of the other get_*_template() functions.
 *
 * @since 1.5.0
 *
 * @param string $type      Filename without extension.
 * @param array  $templates An optional list of template candidates
 * @return string Full path to template file.
 */
function get_query_template( $type, $templates = array() ) {
	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );

	if ( empty( $templates ) )
		$templates = array("{$type}.php");

	$template = locate_template( $templates );

	/**
	 * Filter 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.
	 *
	 * @since 1.5.0
	 *
	 * @param string $template Path to the template. See locate_template().
	 */
	return apply_filters( "{$type}_template", $template );
}