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

发布于 2025-01-07 23:44:46 字数 158 浏览 2 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆离笙 2025-01-14 23:44:46

更新 2019-06-10:在撰写此答案时,没有好的方法可以做到这一点,但从那时起 theme_page_templatestheme_templates 挂钩已添加,这使得这成为可能。从 theme_page_templates 文档页面:

按博客 ID 过滤页面模板

假设您有 ID 为 2 的博客 Food 以及仅应用于该博客的模板 page-food.php。下面的示例从其他博客的下拉列表中删除页面模板:

<代码>/**
 * 过滤主题页面模板。
 *
 * @param array $page_templates 页面模板。
 * @param WP_Theme $这个 WP_Theme 实例。
 * @param WP_Post $post 正在编辑的帖子,提供上下文,或为空。
 * @return array (也许)修改后的页面模板数组。
 */
函数 wpdocs_filter_theme_page_templates( $page_templates, $this, $post ) {
    $current_blog_id = get_current_blog_id();
    $food_blog_id = 2;

    如果($ current_blog_id!= $ food_blog_id){
        if ( isset( $page_templates['page-food.php'] ) ) {
            取消设置( $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]

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 and theme_templates hooks has been added, which makes this possible. From the theme_page_templates docs page:

Filter page templates by blog id

Suppose you have the blog Food with the id 2 and the template page-food.php which should only be used for this blog. The example below removes the page template from dropdowns of other blogs:

/**
 * Filter the theme page templates.
 *
 * @param array    $page_templates Page templates.
 * @param WP_Theme $this           WP_Theme instance.
 * @param WP_Post  $post           The post being edited, provided for context, or null.
 * @return array (Maybe) modified page templates array.
 */
function 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'] );
        }
    }
    return $page_templates;
}
add_filter( 'theme_page_templates', 'wpdocs_filter_theme_page_templates', 20, 3 );> 

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 for get_page_templates() or page_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].

明明#如月 2025-01-14 23:44:46

只需在您的子主题中添加具有相同名称和模板标题的空模板即可。然后使用 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();.

筱果果 2025-01-14 23:44:46

我发现同样的问题有更多答案: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文