diff --git a/packages/block-library/src/categories/block.json b/packages/block-library/src/categories/block.json
index b238df42039e5..448b27c9ccd6d 100644
--- a/packages/block-library/src/categories/block.json
+++ b/packages/block-library/src/categories/block.json
@@ -17,6 +17,10 @@
"showPostCounts": {
"type": "boolean",
"default": false
+ },
+ "showOnlyTopLevel": {
+ "type": "boolean",
+ "default": false
}
},
"supports": {
diff --git a/packages/block-library/src/categories/edit.js b/packages/block-library/src/categories/edit.js
index 475ca9a25954d..08276c026b302 100644
--- a/packages/block-library/src/categories/edit.js
+++ b/packages/block-library/src/categories/edit.js
@@ -21,22 +21,33 @@ import { pin } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
export default function CategoriesEdit( {
- attributes: { displayAsDropdown, showHierarchy, showPostCounts },
+ attributes: {
+ displayAsDropdown,
+ showHierarchy,
+ showPostCounts,
+ showOnlyTopLevel,
+ },
setAttributes,
} ) {
const selectId = useInstanceId( CategoriesEdit, 'blocks-category-select' );
- const { categories, isRequesting } = useSelect( ( select ) => {
- const { getEntityRecords, isResolving } = select( coreStore );
- const query = { per_page: -1, hide_empty: true, context: 'view' };
- return {
- categories: getEntityRecords( 'taxonomy', 'category', query ),
- isRequesting: isResolving( 'getEntityRecords', [
- 'taxonomy',
- 'category',
- query,
- ] ),
- };
- }, [] );
+ const { categories, isRequesting } = useSelect(
+ ( select ) => {
+ const { getEntityRecords, isResolving } = select( coreStore );
+ const query = { per_page: -1, hide_empty: true, context: 'view' };
+ if ( showOnlyTopLevel ) {
+ query.parent = 0;
+ }
+ return {
+ categories: getEntityRecords( 'taxonomy', 'category', query ),
+ isRequesting: isResolving( 'getEntityRecords', [
+ 'taxonomy',
+ 'category',
+ query,
+ ] ),
+ };
+ },
+ [ showOnlyTopLevel ]
+ );
const getCategoriesList = ( parentId ) => {
if ( ! categories?.length ) {
return [];
@@ -133,16 +144,23 @@ export default function CategoriesEdit( {
checked={ displayAsDropdown }
onChange={ toggleAttribute( 'displayAsDropdown' ) }
/>
-
+
+ { ! showOnlyTopLevel && (
+
+ ) }
{ isRequesting && (
diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php
index 3cffff955183c..5492712d5106a 100644
--- a/packages/block-library/src/categories/index.php
+++ b/packages/block-library/src/categories/index.php
@@ -23,6 +23,9 @@ function render_block_core_categories( $attributes ) {
'show_count' => ! empty( $attributes['showPostCounts'] ),
'title_li' => '',
);
+ if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) {
+ $args['parent'] = 0;
+ }
if ( ! empty( $attributes['displayAsDropdown'] ) ) {
$id = 'wp-block-categories-' . $block_id;
diff --git a/test/integration/fixtures/blocks/core__categories.json b/test/integration/fixtures/blocks/core__categories.json
index 71533a5f620fa..825b3d3898066 100644
--- a/test/integration/fixtures/blocks/core__categories.json
+++ b/test/integration/fixtures/blocks/core__categories.json
@@ -6,7 +6,8 @@
"attributes": {
"displayAsDropdown": false,
"showHierarchy": false,
- "showPostCounts": false
+ "showPostCounts": false,
+ "showOnlyTopLevel": false
},
"innerBlocks": [],
"originalContent": ""