问题描述
我正在使用 TwentyTen 主题来创建一个小孩主题,但我似乎无法摆脱 TwentyTen 父主题中的 「One column,no sidebar」 页面模板。
我认为只是复制它,删除内容会做的伎俩,但似乎没有。有谁知道如何做到这一点?我确定这很简单。
谢谢
OSU
最佳解决方案
覆盖该模板会比摆脱它更容易。只是逻辑的方式。
我没有声称它是高效的想法 (在这里晚),但这将得到它从编辑屏幕:
add_action('admin_head-post.php','remove_template');
function remove_template() {
global $wp_themes;
get_themes();
$templates = &$wp_themes['Twenty Ten']['Template Files'];
$template = trailingslashit( TEMPLATEPATH ).'onecolumn-page.php';
$key = array_search($template, $templates);
unset( $templates[$key] );
}
次佳解决方案
WordPress 3.9 引入了 theme_page_templates
滤镜。
来自 Twenty Fourteen 子主题 functions.php
的示例显示如何删除”Contributor Page” 模板:
function tfc_remove_page_templates( $templates ) {
unset( $templates['page-templates/contributors.php'] );
return $templates;
}
add_filter( 'theme_page_templates', 'tfc_remove_page_templates' );
第三种解决方案
扩展 @ Rarst 的答案,这是一个更通用的方法,并不是绑定到一个特定的主题,而是可以在你自己的孩子主题的 functions.php 中使用,以打破你想要摆脱的任何父主题页面模板。
function remove_template( $files_to_delete = array() ){
global $wp_themes;
// As convenience, allow a single value to be used as a scalar without wrapping it in a useless array()
if ( is_scalar( $files_to_delete ) ) $files_to_delete = array( $files_to_delete );
// remove TLA if it was provided
$files_to_delete = preg_replace( "/.[^.]+$/", '', $files_to_delete );
// Populate the global $wp_themes array
get_themes();
$current_theme_name = get_current_theme();
// Note that we're taking a reference to $wp_themes so we can modify it in-place
$template_files = &$wp_themes[$current_theme_name]['Template Files'];
foreach ( $template_files as $file_path ){
foreach( $files_to_delete as $file_name ){
if ( preg_match( '//'.$file_name.'.[^.]+$/', $file_path ) ){
$key = array_search( $file_path, $template_files );
if ( $key ) unset ( $template_files[$key] );
}
}
}
}
所以你可以在你的小孩主题的 functions.php 文件中使用它:
add_action( 'admin_head-post.php', 'remove_parent_templates' );
function remove_parent_templates() {
remove_template( array( "showcase.php", "sidebar-page" ) );
}
这里我只是说明你不必通过”.php” 部分,如果你不想。
或者:remove_template( "sidebar-page" );
– 如果要仅修改单个文件,则不需要传递数组。
第四种方案
WP 核心 (3.9) 中有一个新的过滤器来删除页面模板。它可以从儿童主题中使用。
这是如何在 TwentyTen 中实现的 (在 WP 3.9 中测试):
add_filter( 'theme_page_templates', 'my_remove_page_template' );
function my_remove_page_template( $pages_templates ) {
unset( $pages_templates['onecolumn-page.php'] );
return $pages_templates;
}
https://core.trac.wordpress.org/changeset/27297
http://boiteaweb.fr/theme_page_templates-hook-semaine-16-8033.html
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。