is it possible to use post-type as part of a css selector in block editor stylesheet?

I am working on an editor-stylesheet for the block editor to better reflect the layout of my theme in the editor, and I need to be able to address different post types in there.

In the frontend, there is the body_class() function, which inserts – among others – a class which identifies the post-type and can be used in combined selectors to adress certain elements only in a particular post-type.

The post-type class is there in the body tag of the page which displays the editor, but apparently the editor CSS from the stylesheet is applied in a kind of “isolated” way – combined selectors including classes that are in the body tag won’t work in the editor.

So I am looking for something similar which would work in the block editor, so that I can use it in combined selectors which only apply to certain elements in a particular post-type.

Addition:

I tried to check the post-type this way:

wp.domReady(function() {
        var postType = jQuery('form.metabox-base-form input#post_type').attr('value');
        if(postType == 'post'){
            alert("It's a post!");//in real life some other action...
        }
});

But although it would be logical to at least trigger the alert I put in there, nothing happens when I load the edit page of a post, where that input element including its “post” value is clearly present. (?)

1 Answer
1

In the meanwhile I posted this question also on Stackoverflow. I got a hint in a comment to the question which led me to a solution. I didn’t find a pure CSS solution, but used the following jQuery script to add according classes dynamically:

$(document).ready(function() {
    if($('body').hasClass('post-type-page')) {
        $('#editor').addClass('post-type-page');
    } else if($('body').hasClass('post-type-post')) {
        $('#editor').addClass('post-type-post');
    }
});

Essential was to enqueue that using admin_enqueue_scripts, separately from the frontend scripts. For more details, see https://stackoverflow.com/a/60155398/5641669

Leave a Comment