我想我成功了。
这是从《根上的人》中的圣人主题开始的。
/**
* Here\'s what\'s happening with these hooks:
* 1. WordPress detects theme in themes/sage
* 2. When we activate, we tell WordPress that the theme is actually in themes/sage/templates
* 3. When we call get_template_directory() or get_template_directory_uri(), we point it back to themes/sage
*
* We do this so that the Template Hierarchy will look in themes/sage/templates for core WordPress themes
* But functions.php, style.css, and index.php are all still located in themes/sage
*
* themes/sage/index.php also contains some self-correcting code, just in case the template option gets reset
*/
add_filter(\'stylesheet\', function ($stylesheet) {
return dirname($stylesheet);
});
add_action(\'after_switch_theme\', function () {
$stylesheet = get_option(\'stylesheet\');
if (basename($stylesheet) !== \'templates\') {
update_option(\'stylesheet\', $stylesheet . \'/templates\');
}
});
add_action(\'customize_render_section\', function ($section) {
if ($section->type === \'themes\') {
$section->title = wp_get_theme(basename(__DIR__))->display(\'Name\');
}
}, 10, 2);
我只是在一个新项目上做这件事,所以我不知道它是否会把现有网站上的事情搞砸。虽然我对此深表怀疑,但风险自负。
当您将其粘贴到函数中时。php你必须重新激活你的主题。执行此操作后,看起来没有更改任何设置。一切似乎都很顺利。
我测试了该行为,它基本上会在templates文件夹中查找模板。如果不在那里,它将在默认位置(主题根目录)中查找它,就像正常情况一样。如果没有,它将像普通一样在模板层次结构中移动。