我已经注册了一个自定义块,它基本上是其他内容的包装,带有一些背景颜色选项。
这是我在后端的注册码:
function mytheme_register_box_block() {
register_block_type( \'mytheme/box\', array(
\'attributes\' => array(
\'className\' => array(
\'type\' => \'string\',
\'default\' => \'\',
),
),
\'supports\' => array(
\'customClassName\' => false,
),
\'editor_script\' => \'mytheme-editor\',
) );
}
add_action( \'init\', \'mytheme_register_box_block\' );
以下是我的编辑器脚本导入的块代码:
const { InnerBlocks } = wp.editor;
export default {
title: \'Box\',
description: \'Wrap content within an area to apply a colorscheme to.\',
icon: \'grid-view\',
category: \'layout\',
attributes: {
className: {
type: \'string\',
},
},
styles: [
{
name: \'default\',
label: \'Grey\',
isDefault: true,
},
{
name: \'green\',
label: \'Green\',
},
{
name: \'orange\',
label: \'Orange\',
},
{
name: \'blue\',
label: \'Blue\',
},
{
name: \'cyan\',
label: \'Cyan\',
},
],
edit: props => {
return (
<div className={ props.className }>
{ ( typeof props.insertBlocksAfter !== \'undefined\' ) ? <InnerBlocks /> : <div>Lorem ipsum dolor sit amet.</div> }
</div>
)
},
save: ( { className } ) => {
return (
<div className={ className }>
<InnerBlocks.Content />
</div>
)
},
};
在Gutenberg中,块渲染很好并应用了样式,但当它打印在前端时,只需渲染而不应用样式。
<div class="wp-block-mytheme-box">...</div>
The
attributes
我在register\\u block\\u type调用中添加的列表,我只是想尝试让它工作,但没有用。我不确定我错过了什么。