3

I am using Gutenberg's withSelect package to try to get all of the post types belonging to the site. Here is my code:

export default withSelect( select => {
    // shorthand
    const { getEntityRecords } = select( 'core' );

    return {
        typesList: getEntityRecords( 'types', '' ),
    };
} )( SearchEdit );

But I am not getting anything back. I also found the documentation for withSelect very sparse so if anyone has any further information that would be great and I will try to update the docs.

4

1 回答 1

5

知道自从被问到这个问题已经有一段时间了,但是我今天提出了这个问题,所以这里有一个答案。有一个方法叫做getPostTypes(),见下面的例子:

export default withSelect( select => {
    const {getPostTypes} = select('core);

    return {
        typesList: getPostTypes(),
    }
} )( YourEdit );

注意:getPostTypes()将返回一个帖子类型对象的数组。如果您只需要名称,则需要将结果处理为所需的格式。

于 2019-01-02T19:09:55.673 回答