admin管理员组文章数量:1287581
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. I'm trying to acplish 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.
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. 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. Without good API docs, possible options and parameters are hard to guess.
Here is (part of) my code. I'd like to know the possible parameters for getEntityRecords
in line 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;
} ),
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. I'm trying to acplish 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.
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. 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. Without good API docs, possible options and parameters are hard to guess.
Here is (part of) my code. I'd like to know the possible parameters for getEntityRecords
in line 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;
} ),
Share
Improve this question
asked Dec 17, 2018 at 18:51
oelnaoelna
2,4363 gold badges26 silver badges41 bronze badges
1 Answer
Reset to default 12If you're having the same issue I was: When declaring the custom post type you have to have 'show_in_rest' => true,
since the blocks are based on the restAPI;) Hope this helps
本文标签: javascriptDisplay list of custom post type in Wordpress Gutenberg custom blockStack Overflow
版权声明:本文标题:javascript - Display list of custom post type in Wordpress Gutenberg custom block - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741250513a2365698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论