如何使用php $ content在Gutenberg版本4中呈现InnerBlocks

时间:2018-10-30 16:00:40

标签: wordpress-gutenberg gutenberg-blocks

这对古腾堡3来说很好,

function render_block($attrs, $content){
    ob_start();
        echo $content;
    return ob_get_clean();
}

但是随着gutenberg的最新更新,$content始终为null,即使从save回调中将其传递为:

    save: function(props) {
        return el( InnerBlocks.Content );
    },

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这实际上是古腾堡(Gutenberg)本身的错误,并已在WP 5.1.1中修复

谢谢

答案 1 :(得分:-1)

block.js

(function (blocks, editor, components, i18n, element) {

    var el = element.createElement;

    blocks.registerBlockType('block-category/block-name', {

        ...

        edit: function(props) {

            el(editor.InnerBlocks, {
                allowedBlocks: ['core/heading'],
                template: [['core/heading', {'placeholder':'Placeholder text'}]],
            })

        },

        save: function(props) {

            return el(editor.InnerBlocks.Content);

        }

    });

})(
  window.wp.blocks,
  window.wp.editor,
  window.wp.components,
  window.wp.i18n,
  window.wp.element
);

block.php

add_action('init', function() {

    wp_register_script(
        'block_script',
        plugins_url('block.js', __FILE__),
        array('wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-editor'),
        filemtime(plugin_dir_path(__FILE__) . 'block.js'),
        true
    );

    'editor_script' => 'block_script',

    register_block_type('block-category/block-name', array(

        'render_callback' => function($attributes, $content) {

            return $content;

        }

    ));

});