【问题标题】:Display list of custom post type in Wordpress Gutenberg custom block在 Wordpress Gutenberg 自定义块中显示自定义帖子类型列表
【发布时间】:2019-05-18 04:24:26
【问题描述】:

我正在尝试让我的 Wordpress 插件/主题的用户(它使用自定义帖子类型来处理产品)创建一个显示这些自定义帖子摘要的块。我试图通过在我的插件中创建一个基于official tutorial 的自定义块来实现这一点。在 Gutenberg 后端,我想简单地显示一个选择框,其中包含所有自定义帖子作为选项,但我愿意接受建议。

我试图阅读我可以在块的 javascript 文件中传递给 getEntityRecords 函数的内容,但文档似乎真的很少。如果有人能指出我正确的方向,我将不胜感激。我也尝试设置'taxonomy' 而不是'postType',但它也不起作用。如果没有好的 API 文档,可能的选项和参数很难猜测。

这是我的(部分)代码。我想知道第 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;
} ),

【问题讨论】:

    标签: javascript php wordpress wordpress-gutenberg gutenberg-blocks


    【解决方案1】:

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

    【讨论】:

    • 这确实是最终为我解决的问题,我只是还没有回来回答我自己的问题。感谢您的回答!
    • 首先注册一个 Gutenberg WordPress 自定义类型。该过程相当简单,包括添加以下代码 sn-p。添加 show_in_rest 键并通过您的自定义帖子类型将其设置为 true。 'show_in_rest' => true, 'supports' => array('editor')。整个过程详细定义cloudways.com/blog/gutenberg-wordpress-custom-post-type
    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多