我已经使用register_post_type('myposttype', ...)
注册了一个定制的post类型。在已发布的文章中,有一个自定义类将应用于页面的整个主体,即single-myposttype
。我使用这个body类应用css规则,使post与非自定义post类型的帖子显示不同。
既然Gutenberg编辑器是Wordpress的关键部分,我希望能够在处理这些自定义post类型时向编辑器添加一个自定义body类,以便在编辑过程中应用与发布的文章相同的样式规则。
我已经看到了一些使用javascript的问题(例如这一个),但是如果我正确理解,它们将适用于所有帖子和页面的编辑器,而不仅仅是自定义的post类型。
发布于 2019-02-09 10:49:29
这个钩子应该将一个类添加到编辑器页面的主体中。
add_filter('admin_body_class', function ($classes) {
//get current page
global $pagenow;
//check if the current page is post.php and if the post parameteris set
if ( $pagenow ==='post.php' && isset($_GET['post']) ) {
//get the post type via the post id from the URL
$postType = get_post_type( $_GET['post']);
//append the new class
$classes .= 'single-' . $postType;
}
//next check if this is a new post
elseif ( $pagenow ==='post-new.php' ) {
//check if the post_type parameter is set
if(isset($_GET['post_type'])) {
//in this case you can get the post_type directly from the URL
$classes .= 'single-' . urldecode($_GET['post_type']);
} else {
//if post_type is not set, a 'post' is being created
$classes .= 'single-post';
}
}
return $classes;
});
发布于 2019-02-10 13:07:25
我们可以使用当前屏幕对象将single-{post_type}
添加到它的块编辑器页面的管理主体类中:
add_filter( 'admin_body_class', function ( $classes ) {
$screen = get_current_screen();
return $screen->is_block_editor() && $screen->post_type
? $classes . ' single-' . $screen->post_type
: $classes;
} );
..。但是..。对于编辑风格:
add_theme_support( 'editor-styles' );
add_editor_style( 'style-editor.css' );
那里的CSS将以.editor-styles-wrapper
类选择器作为自动前缀。另外,所有的body
选择器都被.editor-styles-wrapper
替换。我想这是为了使编辑器样式向后兼容,因为它以前是在iframe中加载的,没有手册中提到的任何前缀。
也可以使用enqueue_block_assets
在编辑器管理页面和前端加载样式表,但是如果我们不使用特定的CSS选择器,它可能会破坏整个管理编辑器的布局。因此,我认为这最好用于特定块的目标,而不是一般的布局调整。
https://wordpress.stackexchange.com/questions/328176
复制相似问题