将古腾堡块限制为一次性使用

Restrict Gutenberg Blocks to Single Use

我的块已正确注册并正常运行。
我唯一的抱怨是我不希望在同一个 post.
中多次使用这些块 是否可以像这样限制块行为?文档太糟糕了。

注册自己的积木时,可以将supports属性的multiple选项设置为false:

supports: {
    // Use the block just once per post
    multiple: false
}

来源:https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#multiple.

对于任何也需要此功能的人,以下是将块限制为仅供一次性使用的方法:

// A Sample Block
acf_register_block(array(
    'name'              => 'blockname',
    'title'             => __('Block Name','slug'),
    'description'       => __('A translatable description','slug'),
    'render_callback'   => 'theme_block_render',
    'category'          => 'embed', //the block category (optional)
    'align'             =>  'wide', // default value for width
    'mode'              => 'auto', // default value for switching auomatically between edit and view mode
    'supports'          => array('align' => array('wide','full' ),'multiple' => false), // wide and full are optional, those are for setting the width. SET MULTIPLE TO FALSE HERE!
    'icon'              => 'welcome-view-site', // Use a dashicon, for example dashicons-welcome-view-site
    'keywords'          => array( 'blockname', 'image','text' ), //set some keywords for search
)); 

可以在此处找到更多文档:https://www.advancedcustomfields.com/resources/acf_register_block_type/

在 block.json 中将“多个”设置为 false 在“支持”中:

"category": "widgets",
"icon": "smiley",
"supports": {
    "html": false,
    "multiple": false
},
"attributes": {