简体   繁体   English

在Wordpress Gutenberg自定义块中显示自定义帖子类型的列表

[英]Display list of custom post type in Wordpress Gutenberg custom block

I'm trying to enable users of my Wordpress plugin/theme, which uses a custom post type to handle products, to make a block that displays a summary of one of these custom posts. 我试图让使用自定义帖子类型处理产品的Wordpress插件/主题的用户创建一个块,以显示这些自定义帖子之一的摘要。 I'm trying to accomplish this by creating a custom block in my plugin, based on the official tutorial . 我正在尝试通过在官方教程的基础上在插件中创建一个自定义块来实现这一目标。 On the Gutenberg backend I'd like to simply display a select box with all the custom posts as options, but I'm open to suggestions. 我想在古腾堡(Gutenberg)后端上简单地显示一个带有所有自定义帖子作为选项的选择框,但是我愿意接受建议。

I have tried to read up on what I can pass to the getEntityRecords function in the block's javascript file, but documentation seems really sparse. 我试图阅读我可以传递给该块的javascript文件中的getEntityRecords函数的内容,但是文档似乎确实很少。 If someone could point me in the right direction, I'd really appreciate it. 如果有人能指出正确的方向,我将非常感激。 I have also tried to set 'taxonomy' instead of 'postType' , but it did not work either. 我还尝试设置'taxonomy'而不是'postType' ,但是它也不起作用。 Without good API docs, possible options and parameters are hard to guess. 没有好的API文档,可能的选项和参数很难猜测。

Here is (part of) my code. 这是我的代码(的一部分)。 I'd like to know the possible parameters for getEntityRecords in line 3. 我想知道第3行中getEntityRecords的可能参数。

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;
} ),

如果您遇到相同的问题,我也是:声明自定义帖子类型时,您必须具有'show_in_rest' => true,因为这些块基于restAPI;)希望这对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM