代码之家  ›  专栏  ›  技术社区  ›  oelna

在Wordpress Gutenberg自定义块中显示自定义日志类型列表

  •  0
  • oelna  · 技术社区  · 7 年前

    我正在尝试使用我的WordPress插件/主题(使用自定义文章类型来处理产品)的用户创建一个显示这些自定义文章摘要的块。我正试图通过在插件中创建一个自定义块来实现这一点,基于 official tutorial . 在Gutenberg后端,我只想显示一个带有所有自定义帖子选项的选择框,但是我愿意接受建议。

    我试着去读我能传给 getEntityRecords 函数在块的javascript文件中,但文档似乎非常稀疏。如果有人能给我指明正确的方向,我会非常感激的。我也试过设置 'taxonomy' 而不是 'postType' 但也不起作用。如果没有好的API文档,很难猜测可能的选项和参数。

    这是我的代码的一部分。我想知道 getEntityRecords 在第3行。

    edit: withSelect( function( select ) {
        // setting postType to 'product' does not work for me here
        var pages = select('core').getEntityRecords('postType', 'page', { per_page: 10 });
        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 = [];
        for (var i = 0; i < props.posts.length; i++) {
            var option = el(
                'option',
                { value: props.posts[i].id },
                props.posts[i].title.rendered
            );
            options.push(option);
        }
    
        var select = el(
            'select',
            { className: className },
            options
        );
    
        return select;
    } ),
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Nick Bushman    7 年前

    如果您有相同的问题,我是:当声明自定义的post类型时,您必须 'show_in_rest' => true, 因为块是基于restapi;)希望这有帮助

    推荐文章