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


WordPress Admin Area - Add class to body of page edit screen

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

有没有什么东西可以添加到functions.php(或另一种选择)中,以将该部分从所有页面编辑屏幕中隐藏起来,除了那些特定的屏幕。

我想隐藏的部分div id是#wpcf-post关系,我希望它可见的页面的数据post id是143和23。

-(更新)--

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

简单的方法是使用一个带有"admin_head"钩子的简单CSS规则,如下所示:

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

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

2) 将此代码添加到活动的子主题函数中。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');
    }
}

如果使用主题,请更改:CCD_ 5和CCD_。

另一个类似的(没有外部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>';
    }
}