古腾堡自定义元数据块未将元数据保存为自定义帖子类型

时间:2019-03-01 21:01:19

标签: wordpress wordpress-theming wordpress-rest-api wordpress-gutenberg

我有一个设置了自定义帖子类型的网站,用于定义首页号召性用语。

标题,描述和特色图片均由edtior的默认块/功能处理,但是我试图添加一个自定义块以将URL保存到帖子的元数据中。

该块可正确呈现,但未保存元数据,已明确调用function wb_blocks() { wp_register_script( 'wb-blocks-js', get_template_directory_uri() . '/scripts/block.js', array( 'wp-blocks', 'wp-editor', 'wp-element','wp-components' ) ); register_block_type( 'ray/homebox-link-url', array( 'editor_script' => 'wb-blocks-js', ) ); } add_action( 'init', 'wb_blocks' ); function wb_register_block_meta() { register_meta( 'post', 'homebox_link_url', array( 'show_in_rest' => true, 'single' => true, 'type' => 'string', ) ); } add_action( 'init', 'wb_register_block_meta' ); 函数。

我使用几乎相同的代码为页面和帖子创建自定义元块。这种方法是否不适用于自定义帖子类型?

这是我正在使用的代码:

PHP

registerBlockType( 'ray/homebox-link-url', {
title: 'Homebox Link',
icon: 'universal-access-alt',
category: 'layout',
attributes: {
    blockValue: {
        type: 'string',
        source: 'meta',
        meta: 'homebox_link_url',
    }
},

edit: function( props ) {
    var setAttributes = props.setAttributes;

    function updateBlockValue( blockValue ) {
        setAttributes({ blockValue });
    }

    return el(
       'div',
       { className: "homebox-link-url" },
        el( 'h5',{}, 'URL to link to:'),
        el (TextControl,
        {
            onChange: updateBlockValue,
            value: props.attributes. blockValue,
        })
    );
},

save: function( props ) {
     return null;
},
} );

JS

{{1}}

1 个答案:

答案 0 :(得分:0)

与块相关的代码看起来不错。

问题可能与自定义帖子类型有关。注册时,必须确保它支持自定义字段:

register_post_type(
  'post-type',
  [
    // options...
    'supports' => [
      // ...
      'custom-fields',
    ],
  ]
);

最后一步确保您的自定义帖子类型公开了REST API的元属性,这是古腾堡用来查看/更新数据的内容。

(取自https://github.com/WordPress/gutenberg/issues/5622#issuecomment-375362438

相关问题