admin管理员组

文章数量:1291229

I have different styles applied to pages (I hide the title for them) and posts (I don't want to hide the title here, but style the date of the post). I want to style them differently, which ist possible on the site itself. But in the editor I don't seem to have the possibility to distinguish between pages and posts.

There is no css-class in an upper element which indicates whether I am editing a page or a post which I could use in my css.

How can i know what I am editing in the editor - a page or a post. It's driving me crazy - there must be a way to know this.

I have different styles applied to pages (I hide the title for them) and posts (I don't want to hide the title here, but style the date of the post). I want to style them differently, which ist possible on the site itself. But in the editor I don't seem to have the possibility to distinguish between pages and posts.

There is no css-class in an upper element which indicates whether I am editing a page or a post which I could use in my css.

How can i know what I am editing in the editor - a page or a post. It's driving me crazy - there must be a way to know this.

Share Improve this question asked Jun 7, 2021 at 13:18 Mathias BaderMathias Bader 1357 bronze badges 1
  • 2 What editor are you referring to? – Jacob Peattie Commented Jun 7, 2021 at 13:25
Add a comment  | 

2 Answers 2

Reset to default 2

You can use the admin_body_class hook to add your own CSS classes. For example (if you're using Gutenberg):

function pb_admin_body_class($classes) {
    $screen = get_current_screen();

    if (!$screen->is_block_editor()) {
        return $classes;
    }

    $post_id = isset($_GET['post']) ? intval($_GET['post']) : false;
    $post_type = get_post_type($post_id);

    if ($post_type) {
        $classes .= $post_type;
    }

    return $classes;
}
add_filter('admin_body_class', 'pb_admin_body_class');

The html code must be prepared for it.
For example different classes for the use of css.

example:

<div class="pages">
  <a href="#">title</a>
  <?php whatever the page title code ?>
</div>
<div class="posts">
  <a href="#">title</a>
  <?php whatever the posts title code ?>
</div>

then the css is easy:

 .pages{display:none;}

本文标签: block editorHow to know whether you are editing a page or a post