Wordpress - 隐藏或删除父主题的模板
我正在为 WordPress 制作一个儿童主题。我试图使其对最终用户来说尽可能简单,因此想删除所有父级的页面模板,以便它们不会被意外选择。
有没有办法从子主题功能或插件中取消注册它们?我知道我可以直接从父级中删除它们,但这不是更新/升级的证据。
任何帮助表示赞赏,谢谢!
I'm producing a child theme for wordpress. I'm trying to make it as fool proof as possible for the end users so would like to remove all the parent's page templates so that they can't be accidentally selected.
Is there a way to de-register them from the child themes functions or plugin? I know I could just go and delete them from the parent but that's not update/upgrade proof.
Any help appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新 2019-06-10:在撰写此答案时,没有好的方法可以做到这一点,但从那时起
theme_page_templates
和theme_templates
挂钩已添加,这使得这成为可能。从theme_page_templates
文档页面:上一个答案:
get_themes()
中没有可用的过滤器,WordPress 正是在该位置找到主题中的模板文件和父主题(如果使用)。也没有任何可用于get_page_templates()
或page_template_dropdown()
的过滤器。我想说你最好的选择是以某种方式删除或隐藏管理面板中的选项元素,防止用户选择它们。挂钩
admin_head
操作并注入 javascript 或 css 来处理#page_template 选项[val=template-filename.php]
。UPDATE 2019-06-10: At the time of writing this answer there was no good way of doing this, but since then the
theme_page_templates
andtheme_templates
hooks has been added, which makes this possible. From thetheme_page_templates
docs page:PREVIOUS ANSWER:
There are no filters available in
get_themes()
which is where WordPress locates the template files from the theme and parent theme in case one is used. Nor are there any filters available forget_page_templates()
orpage_template_dropdown()
. I would say your best option is to somehow remove or hide the option elements in the admin panel, preventing the user from choosing them.Hook onto the
admin_head
action and inject javascript or css to handle#page_template option[val=template-filename.php]
.只需在您的子主题中添加具有相同名称和模板标题的空模板即可。然后使用
locate_template();
从这些模板中包含您自己的模板。Simply add empty templates, that have the same name and template header, in your child theme. Then include your own templates from within those template with
locate_template();
.我发现同样的问题有更多答案: https://wordpress.stackexchange.com/questions/13671/how-to-remove-a-parent-theme-page-template-from-a-child-theme
请注意,有些答案并不为...工作WordPress 的更高版本(如 3.8)。 WordPress 3.9 可能有一个钩子,允许开发人员简单地排除或重命名列表中的模板。
I found the same question with more answers: https://wordpress.stackexchange.com/questions/13671/how-to-remove-a-parent-theme-page-template-from-a-child-theme
Note that some answers do not work for later versions of WordPress (like 3.8). WordPress 3.9 might have a hook that will allow developer to simply exclude or rename templates in the list.