更新 – 安德鲁在下面的评论中指出的一个警告:
Use this code with caution, If you have any pages using the template that you removed from the select, updating the page will cause it to revert to the default template
我不确定是否有这样做的PHP方法 – 你必须查看WP_Theme类,但从我第一次看它可能是不可能的,因为为了获得所有模板,该类使用内部函数调用scandir(),它扫描当前主题目录并从那里获取所有.php文件.然后它查找相应的模板名称:标识符,如果它存在,它将被添加到模板列表中.
所以我建议你添加一个JS,它将从页面模板select中删除这个选项.这是一段代码片段:
function my_remove_page_template() {
global $pagenow;
if ( in_array( $pagenow, array( 'post-new.php', 'post.php') ) && get_post_type() == 'page' ) { ?>
(function($){
$(document).ready(function(){
$('#page_template option[value="sidebar-page.php"]').remove();
})
})(jQuery)
}
}
add_action('admin_footer', 'my_remove_page_template', 10);
这将从下拉列表中删除模板sidebar-page.php.条件是这样的,脚本只在页面的添加和编辑屏幕上添加.
调整你的情况,享受:)