Wp.locks.registerBlockType没有在前端显示我想要的内容

时间:2019-08-16 作者:brothman01

据我所知save: 功能是页面前端显示的内容?因此,我试图从我的块中查询数据库,以获取存储在那里的信息,但我不知道如何使用fetch() 中的函数save: 地区

当我在中更改代码时save: 对此,块表示它包含意外或无效的内容,但块仍然有效。

  save: function(props) {
    return wp.element.createElement(
      "div",
      { style: { border: "3px solid " + props.attributes.color } },
      \'foobar baz\'
    );
  }
})
我必须在这里使用道具吗?

1 个回复
SO网友:Welcher

save 属性将导致现有块出现问题,因为静态块将标记保存在save 发布内容。如果进行更改,则需要使用deprecated 属性来告诉古腾堡如何处理save 以及帖子内容中的内容。看见here 对于API。

例如:

registerBlockType( \'blockname\', {
    //existing properties,
    deprecated: [
        {
            attributes: { // whatever your attributes were for the old version }
            save: // the old version of the save function     
         }
      ]
})
至于使用fetch() 函数,您应该能够在返回数据之前按预期在函数中使用它,但我不确定在那里使用它会有什么好处。更好的办法是在edit 属性并将数据设置为attribute 街区的。

伪代码:

edit: ( props ) => {
    fetch() // not sure what we\'re getting here so...?
    .then(data => {
        if ( data.thing ) {
            this.setAttributes( { attName: data.thing } )
        }
    })

}

save: function(props) {
    return wp.element.createElement(
      "div",
      { style: { border: "3px solid " + props.attributes.color } },
      {props.attributes.attName}
    );
  }
})
希望有帮助!

相关推荐

Do not parse shortcode in CPT

我有一个CPT,我不想在它的内容中解析shortcode(使用\\u content()函数)。我可以使用remove\\u filter删除短代码的默认过滤器。但我如何确定我只是为了我想要的CPT而删除过滤器?我有一个在页面中使用的快捷码[我的自定义快捷码]。此短代码使用WP\\U查询和输出CPT帖子。我不想在这篇CPT文章中分析短代码。我是否应该在短代码解析挂钩之前用虚拟内容更改短代码,并在之后替换回来?或者我应该在我的CPT输出之前删除短代码的默认过滤器,然后在我的CPT输出完成后再次添加短代码的默