get_query_template()获取没有扩展名的模版文件

目录

描述

译文

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

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

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
  if (  != get_query_template( '404' ) )         
      include( get_query_template( '404' ) );  

以下用法也可以达到同样效果:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
 if (  != get_404_template() )         
     include( get_404_template() );  

原文

Retrieve path to file without the use of extension.

Used to quickly retrieve the path of file without including the file extension. It will also check the parent template, if the file exists, with the use of locate_template(). Allows for more generic file location without the use of the other get_*_template() functions.

Can be used with include() or require() to retrieve path.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
   if ( '' != get_query_template( '404' ) )
       include( get_query_template( '404' ) );

or the same can be accomplished with

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
   if ( '' != get_404_template() )
       include( get_404_template() );

get_query_template() 描述

用法

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

get_query_template() 用法

参数

$type

(string) (必填) Filename without extension.

默认值: None

$templates

(array) (可选) An optional list of template candidates

默认值: array()

get_query_template() 参数

返回值

(string) 

Full path to file.

get_query_template() 返回值

注意

get_query_template() 注意

历史

添加于 版本: 1.5.0

get_query_template() 历史

源文件

get_query_template() 函数的代码位于 wp-includes/template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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 );
}

get_query_template() 源文件