个性化阅读
专注于IT技术分析

WordPress-隐藏或删除父主题的模板

点击下载

我正在为Wordpress制作一个子主题。我试图为最终用户提供尽可能简单的证明, 因此希望删除所有父级的页面模板, 以免意外选择它们。

有没有办法从子主题功能或插件中注销它们?我知道我可以直接从父级删除它们, 但这不是更新/升级的证明。

任何帮助表示赞赏, 谢谢!


#1


更新2019-06-10:在编写此答案时, 没有做到这一点的好方法, 但是自那时以来, 已经添加了theme_page_templates和theme_templates挂钩, 这使之成为可能。在theme_page_templates文档页面中:

按博客ID过滤页面模板假设你有ID为2的博客Food和仅应用于该博客的模板page-food.php。以下示例从其他博客的下拉列表中删除了页面模板:/ ** *过滤主题页面模板。 * * @param array $ page_templates页面模板。 * @param WP_Theme $此WP_Theme实例。 * @param WP_Post $ post正在编辑的帖子, 为上下文提供, 或者为null。 * @return数组(也许)已修改的页面模板数组。 * /函数wpdocs_filter_theme_page_templates($ page_templates, $ this, $ post){$ current_blog_id = get_current_blog_id(); $ food_blog_id = 2; if($ current_blog_id!= $ food_blog_id){if(isset($ page_templates [‘page-food.php’])){unset($ page_templates [‘page-food.php’]);返回$ page_templates; } add_filter(‘theme_page_templates’, ‘wpdocs_filter_theme_page_templates’, 20, 3);>


上一个答案:

get_themes()中没有可用的过滤器, 在这种情况下, WordPress会从主题和父主题中查找模板文件(以防万一)。 get_page_templates()或page_template_dropdown()也没有任何过滤器。我想说, 最好的选择是以某种方式删除或隐藏管理面板中的选项元素, 以防止用户选择它们。

钩上admin_head操作并注入javascript或css来处理#page_template选项[val = template-filename.php]。


#2


只需在子主题中添加具有相同名称和模板标题的空模板。然后使用locate_template();从这些模板中包含你自己的模板。


#3


我发现了具有更多答案的相同问题:https://wordpress.stackexchange.com/questions/13671/how-to-remove-a-parent-theme-page-template-from-a-child-theme

请注意, 某些答案不适用于更高版本的WordPress(例如3.8)。 WordPress 3.9可能有一个钩子, 使开发人员可以简单地排除或重命名列表中的模板。

赞(0)
未经允许不得转载:srcmini » WordPress-隐藏或删除父主题的模板

评论 抢沙发

评论前必须登录!