首页 文章

WordPress管理区域 - 将类添加到页面编辑屏幕的主体

提问于
浏览
3

我使用wp-types工具集来创建自定义帖子类型和页面的帖子关系;现在每个页面编辑屏幕的底部都有一个帖子关系部分 . 问题是,我只希望这一部分出现在几页上 .

有什么东西我可以添加到functions.php(或其他替代方案)来隐藏所有页面编辑屏幕中的这一部分 .

我要隐藏的部分div是#wpcf-post-relationship,我希望它可见的页面的数据post id是143和23 .

1 回答

  • 2
    • (更新) -

    当用户访问管理区域时,在任何其他挂钩之前触发admin_init,我们最终使用admin_head,因为只是在管理页面的<head>内触发了操作(感谢John) .

    简单的方法是使用带有'admin_head'钩子的简单CSS规则来执行此操作,如下所示:

    1)创建一个名为 hide_some_field.css 的css文件,并将其放入活动的子主题文件夹中,使用以下代码:

    #wpcf-post-relationship {
        display:none;
    }
    

    2)在您的活动子主题functions.php文件中添加此代码:

    add_action('admin_head', 'ts_hiding_some_fields');
    function ts_hiding_some_fields(){
        // your 2 pages in this array
        $arr = array(23, 143);
        if(get_post_type() == 'page' && !in_array(get_the_ID(), $arr))
        {
            wp_enqueue_style( 'hide_some_field', get_stylesheet_directory_uri().'/hide_some_field.css');
        }
    }
    

    如果您改为使用 theme ,请通过 get_template_directory_uri() 更改: get_stylesheet_directory_uri() .

    另一个类似的替代方案(没有外部CSS文件)是:

    add_action('admin_head', 'ts_hiding_some_fields');
    function ts_hiding_some_fields(){
        // your 2 pages in this array
        $arr = array(23, 143);
        if(get_post_type() == 'page' && !in_array(get_the_ID(), $arr))
        {
            echo '<style type="text/css">
            #wpcf-post-relationship {display: none;}
            </style>';
        }
    }
    

相关问题