如何在Gutenberg编辑器中获取所有自定义帖子类型的帖子ID

时间:2019-02-20 作者:billal

我想访问自定义帖子类型的所有帖子的IDbook 在我的定制古腾堡街区voucher 然后在option list 在管理端。我想要实现的是当我点击voucher 块,则应在管理编辑器上显示一个选择标记,其中包含类型为的帖子id列表voucher. 我读过Dynamic block gutenbeg 文档,但无法了解如何获取自定义帖子类型的帖子id。

这是我的密码。

wp.blocks.registerBlockType(\'voucher/shortcode\', {
    title: \'Voucher Shortcode\',
    icon: \'smiley\',
    category: \'common\',
    attributes: {
        category: {
            type: \'string\',
        }
    },
    edit: function(props) { 
        return  React.createElement(
                "div",
                null,
                React.createElement(
                    "select",
                    null,
                    React.createElement(
                    "option",
                    {
                        value: "animals"
                    },
                    "Animals"
                    ),
                    React.createElement(
                    "option",
                    {
                        value: "arch"
                    },
                    "Architecture"
                    ),
                    React.createElement(
                    "option",
                    {
                        value: "nature"
                    },
                    "Nature"
                    ),
                    React.createElement(
                    "option",
                    {
                        value: "people"
                    },
                    "People"
                    ),
                    React.createElement(
                    "option",
                    {
                        value: "tech"
                    },
                    "Tech"
                    )
                )
                );

    },
    save: function() {

          return null;
    } 
})

1 个回复
SO网友:moped

var el = wp.element.createElement
var withSelect = wp.data.withSelect

wp.blocks.registerBlockType(\'voucher/shortcode\', {
title: \'Voucher Shortcode\',
icon: \'smiley\',
category: \'common\',
attributes: {
    category: {
        type: \'string\',
    }
},

edit: withSelect( function( select ) {
            var pages = select(\'core\').getEntityRecords(\'postType\', \'voucher\', { per_page: -1 });

            return {
                posts: pages
            };
        } )

     (function(props) { 

            if ( ! props.posts ) {
                return "Loading...";
            }

            if ( props.posts.length === 0 ) {
                return "No posts";
            }
            var className = props.className;
            var post = props.posts[ 0 ];

            var options = [];
            var option = el(
                            \'option\',
                            { value: \'\' },
                            __(\'Choose\')
                            );
            options.push(option);

            for (var i = 0; i < props.posts.length; i++) {
                 option = el(
                                \'option\',
                                { value: props.posts[i].id },
                                props.posts[i].title.raw
                                );
                options.push(option);
            }

            return [
                    el(\'select\', {
                        onChange: onSelectVoucher,
                        },
                        options
                        )
            ]
    }),

    save: function() {

          return null;
    } })
您可以尝试使用CPTs凭证创建动态选择字段
事件功能onChange: onSelectVoucher 该怎么办由你决定。

相关推荐