【发布时间】:2021-03-14 01:29:47
【问题描述】:
我正在尝试将 Wordpress 的模板文件重新映射到我的主题中的子目录(类似于 views 文件夹)。我尝试了一些在 Wordpress 文档中找到的钩子,但没有按预期工作。
分析“包含”主题模板文件的核心函数,我在 wp-includes/template.php 文件中找到了locate_template 函数。我想重写那个函数来达到我想要的。
所以,原来的函数体是:
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
$located = STYLESHEETPATH . '/' . $template_name;
break;
} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
$located = TEMPLATEPATH . '/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once, $args );
}
return $located;
}
覆盖只是将views/ 附加到 Wordpress 搜索模板文件的路径,如下所示:
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( STYLESHEETPATH . '/views/' . $template_name ) ) {
$located = STYLESHEETPATH . '/views/' . $template_name;
break;
} elseif ( file_exists( TEMPLATEPATH . '/views/' . $template_name ) ) {
$located = TEMPLATEPATH . '/views/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once, $args );
}
return $located;
}
我知道修改核心函数是一个(大)错误,所以我想要的是从我的主题的 functions.php 文件中覆盖这个函数。我想我只需要在一些初始化钩子上重新声明这个函数,但我找不到要使用的钩子(如果这是正确的方法)。
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: php wordpress wordpress-theming