WordPress模板函数在默认情况下不支持传递变量(据我所知),因此您需要编写自己的函数。比如像这样,
// functions.php
function my_template_include_with_variables( string $path = \'\', $data = null ) {
$file = get_template_directory() . \'/\' . $path . \'.php\';
if ( $path && file_exists( $file ) ) {
// $data variable is now available in the other file
include $file;
}
}
// in a template file
$critical_css = \'style\';
// include header.php and have $critical_css available in the template file
my_template_include_with_variables(\'header\', $critical_css);
// in header.php
var_dump($data); // should contain whatever $critical_css had
<小时/>
UPDATE 18.8.2020
自从
WordPress 5.5 您可以通过本机模板函数传递其他参数(例如。
get_header(),
get_template_part(), 数组中的模板文件。
来自开发文档,
get\\u template\\u part(字符串$slug,字符串$name=null,数组$args=array())