我正在尝试将Wordpress的模板文件重新映射到主题中的子目录(类似于views文件夹)。我尝试过在Wordpress文档中找到一些钩子,但是没有什么能像预期的那样工作。
通过分析“包含”主题模板文件的核心函数,我在wp-include/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文件中重写这个函数。我想我只需要在某个初始化钩子上重新声明这个函数,但是我找不到使用哪个钩子(如果这是正确的方法)。
有人能帮我吗?
发布于 2021-03-14 02:53:52
“我正在尝试将Wordpress的模板文件重新映射到主题中的子目录中”
为什么?用一个孩子的主题。其他任何东西都会很脆弱,容易出错。
考虑使用htaccess文件进行重新映射。
https://stackoverflow.com/questions/66620233
复制相似问题