Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new option to show or hide empty categories #39426

Merged
merged 4 commits into from Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Expand Up @@ -69,7 +69,7 @@ Display a list of all categories. ([Source](https://github.com/WordPress/gutenbe
- **Name:** core/categories
- **Category:** widgets
- **Supports:** align, ~~html~~
- **Attributes:** displayAsDropdown, showHierarchy, showOnlyTopLevel, showPostCounts
- **Attributes:** displayAsDropdown, showEmpty, showHierarchy, showOnlyTopLevel, showPostCounts

## Code

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/categories/block.json
Expand Up @@ -22,6 +22,10 @@
"showOnlyTopLevel": {
"type": "boolean",
"default": false
},
"showEmpty": {
"type": "boolean",
"default": false
}
},
"supports": {
Expand Down
8 changes: 7 additions & 1 deletion packages/block-library/src/categories/edit.js
Expand Up @@ -25,11 +25,12 @@ export default function CategoriesEdit( {
showHierarchy,
showPostCounts,
showOnlyTopLevel,
showEmpty,
},
setAttributes,
} ) {
const selectId = useInstanceId( CategoriesEdit, 'blocks-category-select' );
const query = { per_page: -1, hide_empty: true, context: 'view' };
const query = { per_page: -1, hide_empty: ! showEmpty, context: 'view' };
if ( showOnlyTopLevel ) {
query.parent = 0;
}
Expand Down Expand Up @@ -144,6 +145,11 @@ export default function CategoriesEdit( {
checked={ showOnlyTopLevel }
onChange={ toggleAttribute( 'showOnlyTopLevel' ) }
/>
<ToggleControl
label={ __( 'Show empty categories' ) }
checked={ showEmpty }
onChange={ toggleAttribute( 'showEmpty' ) }
/>
{ ! showOnlyTopLevel && (
<ToggleControl
label={ __( 'Show hierarchy' ) }
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/categories/index.php
Expand Up @@ -22,6 +22,7 @@ function render_block_core_categories( $attributes ) {
'orderby' => 'name',
'show_count' => ! empty( $attributes['showPostCounts'] ),
'title_li' => '',
'hide_empty' => empty( $attributes['showEmpty'] ),
);
if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) {
$args['parent'] = 0;
Expand Down
3 changes: 2 additions & 1 deletion test/integration/fixtures/blocks/core__categories.json
Expand Up @@ -6,7 +6,8 @@
"displayAsDropdown": false,
"showHierarchy": false,
"showPostCounts": false,
"showOnlyTopLevel": false
"showOnlyTopLevel": false,
"showEmpty": false
},
"innerBlocks": []
}
Expand Down