首先,让我们通过一些注释来查看您的代码,以显示发生了什么。
/**
* Adds the function "set_templates" to the "template_include" filter with a
* prioritiy of 99 (default is 10).
*
* What this means is that whenever WordPress runs the filter "template_link",
* your function "set_templates" will be called and the value that is about to
* be returned for "template_link" could be modified by your code.
*
* In WordPress, this fitler gets called in wp-includes/template-loader.php
* after WordPress does all of it\'s internal "magic" to figure out which template
* to use.
*
* The priority of 99 tells WordPress to run the filter later than most other
* filter functions, meaning yours will likely be the last to run unless you
* have another plugin somewhere with a higher (>99) priority.
*/
add_filter( \'template_include\', \'set_templates\', 99 );
/**
* Maybe set a template if we\'re on certain pages.
*
* @param string $template The template that WP is going to use.
* @return string A template file path
*/
function set_templates( $template ) {
// Initialize the $new_template variable to an empty string.
$new_template = \'\';
// is_page will return "true" if the passed parameter matches the current page query.
if ( is_page( \'example_1\' ) ) {
// If we match the query for \'example_1\', for example, then we set the
// new template path to the value of locate_template.
$new_template = locate_template( \'templates/example-1.php\' ) );
} elseif ( is_page( \'example_2\' ) ) {
$new_template = locate_template( \'templates/example-2.php\' ) );
} elseif ( is_single( \'example_3\' ) ) {
$new_template = locate_template( \'templates/example-3.php\' ) );
}
// If we get a value for the new template, stop here and return it.
if ( \'\' !== $new_template ) {
return $new_template ;
}
// Otherwise, return the original template.
return $template;
}
我的代码和您的原始代码有一个重要的区别,那就是我们正在设置
$new_template = \'\'
在函数开始时,对空字符串的检查从松散的比较更改为
!=
进行严格的比较
!==
.
这样做的原因是,松散的比较本质上允许您做一些依赖于PHP的事情,只是“随波逐流”。事先,说你从来没有打过locate_template
块-$new_template
永远不会被定义。对未定义的空字符串进行松散检查是可行的,但这也可能会给PHP带来一些问题:
php -a
Interactive shell
php > var_dump($not_defined == \'\');
PHP Notice: Undefined variable: not_real in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
Notice: Undefined variable: not_real in php shell code on line 1
Call Stack:
7.2574 352080 1. {main}() php shell code:0
php shell code:1:
bool(true)
php >
正如你所见,
$not_defined
大致等于空字符串
\'\'
, 然而PHP抛出了
Notice
因为
$not_defined
从未定义。在您的情况下,设置
$new_template
首先清空字符串可以避免任何PHP投诉,并且
locate_template
返回模板路径的字符串或空字符串
\'\'
, 最后我们可以进行严格的比较。
要回答您关于这种比较的问题,$new_template !== \'\'
是说“如果我们对$new_template
这是一个非空字符串,请返回该字符串。“否则,控制流将通过if
语句块并返回原始$template
WordPress最初计划使用。
add_filter
- https://developer.wordpress.org/reference/functions/add_filter/is_page
- https://developer.wordpress.org/reference/functions/is_page/locate_template
- https://developer.wordpress.org/reference/functions/locate_template/ 替代方案(更新)
我想我还想提到,如果您愿意稍微更改一下文件结构,就可以实现类似的目标。如果文件名与格式“Page-.PHP”匹配,WordPress将查找与页面段塞匹配的PHP文件,因此在您的示例中,您可以使用以下结构:
> ls wp-content
... // Other files
page-example-1.php
page-example-2.php
page-example-3.php