您需要使用Custom Post Type 来表示块。它的作用是您可以注册自己的帖子模板,其中包含预定义的块。我相信这就是您正在寻找的。这是一个例子 -
function myplugin_register_book_post_type() {
$args = array(
'public' => true,
'label' => 'Books',
'show_in_rest' => true,
'template' => array(
array( 'core/image', array(
'align' => 'left',
) ),
array( 'core/heading', array(
'placeholder' => 'Add Author...',
) ),
array( 'core/paragraph', array(
'placeholder' => 'Add Description...',
) ),
),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'myplugin_register_book_post_type' );
这个函数注册一个 CPT 和 via 模板选项,指定哪个块在里面。您可以锁定(不允许外部块)、松散锁定(允许某些块)或允许插入其他块。