【问题标题】:Restrict Gutenberg Blocks to Single Use将古腾堡街区限制为一次性使用
【发布时间】:2019-11-07 18:13:34
【问题描述】:

我的块已正确注册并正常运行。我唯一需要注意的是,我不希望在同一个帖子中使用这些块超过 1(一)次。是否可以限制这样的阻止行为?文档太可怕了。

提前致谢

【问题讨论】:

  • 您可以尝试使用core/block-editor中的wp.data.select("core/editor").getBlocks()探索块编辑器的数据,并使用wp.data.dispatch("core/editor").lockPostSaving("lock_key")来阻止帖子保存...

标签: wordpress-gutenberg


【解决方案1】:

在 block.json 中将“multiple”设置为 false 在“supports”中:

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

【讨论】:

    【解决方案2】:

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

    // 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/

    【讨论】:

      【解决方案3】:

      如果您要注册自己的块,您可以在注册块时将multiplesupports 选项设置为false,请参阅此处的官方文档:https://developer.wordpress.org/block-editor/developers/block-api/block-registration/#supports-optional

      【讨论】:

        猜你喜欢
        • 2019-02-16
        • 2021-08-24
        • 2019-08-02
        • 2021-07-05
        • 1970-01-01
        • 2011-04-02
        • 2022-12-15
        • 1970-01-01
        • 2022-10-22
        相关资源
        最近更新 更多