编辑自定义模板选择器
问题描述:
我试图在目录和子目录上组织自定义页面模板,但我无法让该wordpress识别它们。编辑自定义模板选择器
我已经有一个递归函数来获得关联数组与模板路径作为键和模板名称作为值,并修改wp缓存,页面模板选择器捕获它们,但是当页面保存时,后meta包含其他模板;(
获取模板功能
function get_custom_templates($path = null, $templates = array()) {
$parent = "";
if (is_null($path))
$path = get_stylesheet_directory() . "/custom_templates";
else
$parent = basename($path);
foreach (scandir($path) as $item) {
if (substr($item, 0, 1) == ".") continue;
$template = $path . "/" . $item;
if (is_dir($template)) {
$templates = array_merge($templates, get_custom_templates($template, $templates));
} else {
$templates[$template] = $parent . "-" . basename($template, ".php");
}
}
return $templates;
}
它们添加到编辑
function include_custom_templates($atts) {
$cache_key = "page_templates-" . md5(get_theme_root() . "/" . get_stylesheet());
$templates = wp_get_theme()->get_page_templates();
if (empty($templates))
$templates = array();
wp_cache_delete($cache_key, "themes");
$templates = array_merge($templates, get_custom_templates());
wp_cache_add($cache_key, $templates, "themes", 1800);
return $attrs;
}
add_filter("page_attributes_dropdown_pages_args", "include_custom_templates");
add_filter("wp_insert_post_data", "include_custom_templates");
答
WordPress的识别子页面的模板。因此,将全局页面模板存储在此文件夹中以帮助保持其组织性是一个好主意。
自从WP 3.4以来一直如此。
所以我的建议是把它们放在你主题的/page-templates
子目录中,而不是/custom_templates
。这样,你就能让WordPress立即识别它们。
是的,我知道,但这个功能不允许其中的子目录。我的模板具有许多视图的自定义类型。 –