Wordpress Gutenberg anchor 支持动态 block

标签 wordpress wordpress-gutenberg

我想为我的动态 wordpress block 提供 anchor 支持。我做了

//in registerBlockType
supports: {
    anchor: true,
},

这会在侧边栏面板下添加 HTML anchor 控件。

我的 block 是一个动态 block ,有

save: ( props ) => {
  return <InnerBlocks.Content />;
}

我尝试了所有方法来将 anchor 属性添加到前端。根据this github issue我应该补充

anchor: { 
    type: 'string', 
    source: 'attribute', 
    attribute: 'id', 
    selector: '*', 
}, 

block 属性。这将使 anchor 通过 props.anchorsave 函数中可用,但是它从未出现在我的 render_callback$属性

这基本上是 github 问题到 SO 的端口。希望任何人都可以在这里提供帮助。

最佳答案

如果有人仍然对此感兴趣,这对我有用:

所以这是我的自定义 block 注册,此语句将在所选古腾堡 block 的“高级”选项卡下启用标准 wordpress HTML anchor 字段(对空格等进行有值(value)的验证):

supports: {
  anchor: true
}

然后在同一个地方我们定义:

attributes: {
  anchor: {
    type: 'string'
  }
}

然后在保存函数中(我使用它的目的与 InnerBlocks 完全相同):

save: function(props) {
  const { anchor } = props.attributes;
  return (
    el( anchor, {}),
    el( InnerBlocks.Content, {})
  );
}

如果您使用的是 jsx,保存函数可能如下所示:

save: function(props) {
  const { anchor } = props.attributes;
  return (
    <div id={anchor}>
      <InnerBlocks.Content />
    </div>
  );
}

然后在您的渲染回调函数中(在 php 中)它将通过第一个 arg 的(数组)元素可用

function your_callback( $block, $content ) {
  // display your anchor value
  echo $block['anchor'];
}


关于Wordpress Gutenberg anchor 支持动态 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56656937/

相关文章:

php - 从 Woocommerce 3.4+ 的结帐字段中删除 "(optional)"文本

wordpress - WordPress在哪里存储自定义菜单?

WordPress 古腾堡 : This block contains unexpected or invalid content

wordpress - 在 gutenberg 编辑器中将类添加到父 .wp-block 元素

html - 在 flexed div 的中间垂直对齐内容

javascript - 在 WordPress Gutenberg block 中调用 DateTimePicker

php - Wordpress wpdb->从表单准备 sql 字符串

php - 将侧边栏与我的主要内容 Bootstrap 对齐

php - 如何在wordpress静态页面中添加自定义div

css - WordPress 5.9 editor-style.css 不再工作并添加了覆盖 block 样式的 css 属性