如何在 Wordpress 中隐藏模板? [英] How to hide a template in Wordpress?

查看:40
本文介绍了如何在 Wordpress 中隐藏模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在管理员中隐藏模板文件?

Is there a way to hide a template file in admin?

例如,我有一个模板,只有在安装了特定插件时才应该可用,而且我已经知道如何检查插件是否处于活动状态.但是如何隐藏模板?

For example I have a template that should only be available if a specific plugin is installed, and I already know how to check if plugin is active. But how do I hide the template?

例如我想隐藏Blogger Redirection"-模板波纹管:

For example I want to hide "Blogger Redirection"-template bellow:

我找到了几个链接,但所有解决方案似乎都已弃用.

I have found several links, but all of the solutions seems deprecated.

如果有人对我如何检查插件是否处于活动状态感兴趣,我会使用以下功能进行:

function isPluginActive($plugin){
        if ( in_array( $plugin, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
        {
            return true;
        }
        return false;
    }

推荐答案

更新 - 安德鲁在下面的评论中指出的一个警告:

谨慎使用此代码,如果您有任何页面使用从选择中删除的模板,更新页面将使其恢复为默认模板

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 文件.然后查找相应的 Template Name: 标识符,如果存在,则将其添加到模板列表中.


I'm not sure if there is a PHP way of doing this - you'll have to look in the WP_Theme class, but from my first look it might not be possible because in order to get all templates, the class utilizes an internal function called scandir() which scans the current theme directory and gets all .php files from there. It then looks for the corresponding Template Name: identifier and if it's present it gets added to the templates list.

因此,我建议您添加一些 JS 以从页面模板选择中删除此选项.这是一个代码片段:

So instead I suggest, that you add a little JS that will remove this option from the page template select. Here's a code snippet:

function my_remove_page_template() {
    global $pagenow;
    if ( in_array( $pagenow, array( 'post-new.php', 'post.php') ) && get_post_type() == 'page' ) { ?>
        <script>
            (function($){
                $(document).ready(function(){
                    $('#page_template option[value="sidebar-page.php"]').remove();
                })
            })(jQuery)
        </script>
    <?php 
    }
}
add_action('admin_footer', 'my_remove_page_template', 10);

这将从下拉列表中删除模板 sidebar-page.php.条件是只在页面的添加和编辑屏幕上添加脚本.

This will remove the template sidebar-page.php from the dropdown. The conditionals are so that the script is only added on add and edit screens of pages.

根据您的情况调整并享受:)

Adjust to your case and enjoy :)

这篇关于如何在 Wordpress 中隐藏模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆