如何在cutsom gutenberg块中添加块内容

时间:2019-11-22 10:20:11

标签: wordpress wordpress-gutenberg gutenberg-blocks create-guten-block

当我的自定义块添加到页面时,我试图在管理面板中添加自定义的简码。 enter image description here

( function( blocks, element, editor, components) {
    var el = element.createElement;

const { RichText, InspectorControls, BlockEdit } = editor;
const { Fragment } = element;
const { TextControl, ToggleControl, Panel, PanelBody, PanelRow, SelectControl } = components;

blocks.registerBlockType( 'gutenberg-custom/show-information', {
    title: 'Show information',
    icon: 'universal-access-alt',
    category: 'layout',
    example: {},
    attributes: {
      exhibitor_id:{
        type: "string"
      }
    },
    edit: function(props) {
            console.log('props', props)
              return el(
                Fragment,
                {},
                el(
                  InspectorControls,
                  {},
                  el(
                    PanelBody,
                    { title: "Show Settings", initialOpen: true },
                    /* Text Field */
                    el(
                      PanelRow,
                      {},
                      el(SelectControl, {
                        label: "Select Exhibitor",
                        options:  [
                            { label: 'Big', value: '100%' },
                            { label: 'Medium', value: '50%' },
                            { label: 'Small', value: '25%' },
                        ] ,
                        onChange: value => {
                          props.setAttributes({ exhibitor_id: value });
                        },
                        value: props.attributes.exhibitor_id
                      })
                    )
                  ),
                 )
              );
            },
    save: function(props){
              return 'Test output'
          }
} );
}(
    window.wp.blocks,
    window.wp.element,
    window.wp.editor,
    window.wp.components
) );

以上是我的js代码。我可以为该块添加设置,但无法弄清楚如何在块本身中添加自定义内容“ [custom-shortcode]”(如图所示)。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

edit: function(props) {
      console.log("props", props);
      return el(
        Fragment,
        {},
        el(
          InspectorControls,
          {},
          el(
            PanelBody,
            { title: "Show Settings", initialOpen: true },
            el(
              PanelRow,
              {},
              el(SelectControl, {
                label: "Select Exhibitor",
                options: [
                  { label: "Big", value: "100%" },
                  { label: "Medium", value: "50%" },
                  { label: "Small", value: "25%" }
                ],
                onChange: value => {
                  props.setAttributes({ exhibitor_id: value });
                },
                value: props.attributes.exhibitor_id
              })
            )
          )
        ),
        el(
          "div",
          {},
          "[show-information]"
        )
      );
    },