admin管理员组文章数量:1193808
Is that possible right now?
Running:
wp.data.select('core').getEntityRecords('root', 'taxonomy')
Lists all registered taxonomies, same result with:
wp.data.select('core').getTaxonomies()
Can I pass post type somehow? I want to populate a SelectControl
with taxonomies associated with the post, showing all taxonomies will be confusing.
Is that possible right now?
Running:
wp.data.select('core').getEntityRecords('root', 'taxonomy')
Lists all registered taxonomies, same result with:
wp.data.select('core').getTaxonomies()
Can I pass post type somehow? I want to populate a SelectControl
with taxonomies associated with the post, showing all taxonomies will be confusing.
1 Answer
Reset to default 2Yes, you can pass a post type to getEntityRecords()
and also getTaxonomies()
, both using the same format which is an object of arguments:
Using
getEntityRecords()
:wp.data.select( 'core' ).getEntityRecords( 'root', 'taxonomy', { type: 'post' } )
Using
getTaxonomies()
: ( which is actually a shorthand for the above code )wp.data.select( 'core' ).getTaxonomies( { type: 'post' } )
See the REST API handbook for the list of accepted arguments: https://developer.wordpress.org/rest-api/reference/taxonomies/
Working example using getTaxonomies()
So this is for a block type with a string attribute named taxonomy
(which stores the taxonomy slug), and I'm using useSelect
:
function edit( { attributes, setAttributes } ) {
// just an example which defines a dynamic post type
const postType = useSelect( select => {
const { getCurrentPostType } = select( 'core/editor' );
return getCurrentPostType();
}, [] );
// fetch the post type's taxonomies from the REST API, or retrieve them from the cache
const taxonomies = useSelect( select => {
const { getTaxonomies } = select( 'core' );
return getTaxonomies( { type: postType } );
}, [ postType ] );
// build the options object for SelectControl - remember that `taxonomies` could be a
// null if the REST API request hasn't yet been fully resolved
const options = taxonomies?.map( taxonomy => ( {
label: `${ taxonomy.name } (${ taxonomy.slug })`,
value: taxonomy.slug,
} ) );
return (
<div { ...useBlockProps() }>
<SelectControl
label="Taxonomy"
value={ attributes.taxonomy }
options={ options }
onChange={ value => setAttributes( { taxonomy: value } ) }
__nextHasNoMarginBottom
/>
</div>
);
}
本文标签: custom taxonomyGetting taxonomies associated with a specified post type
版权声明:本文标题:custom taxonomy - Getting taxonomies associated with a specified post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738487314a2089498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论