Skip to content

Commit

Permalink
Navigation block: Add current-menu-item class for post type archive (#…
Browse files Browse the repository at this point in the history
…57808)

* Add current-menu-item class if the current page is a post type archive

Unlinked contributors: Gierand.

Co-authored-by: carolinan <[email protected]>
Co-authored-by: fabiankaegy <[email protected]>
Co-authored-by: Humanify-nl <[email protected]>
  • Loading branch information
4 people authored Apr 19, 2024
1 parent 9912c11 commit 4b08cf1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
$kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] );
$is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind );

if ( is_post_type_archive() ) {
$queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
if ( $attributes['url'] === $queried_archive_link ) {
$is_active = true;
}
}

$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/navigation-submenu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
$kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] );
$is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind );

if ( is_post_type_archive() ) {
$queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
if ( $attributes['url'] === $queried_archive_link ) {
$is_active = true;
}
}

$show_submenu_indicators = isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'];
$open_on_click = isset( $block->context['openSubmenusOnClick'] ) && $block->context['openSubmenusOnClick'];
$open_on_hover_and_click = isset( $block->context['openSubmenusOnClick'] ) && ! $block->context['openSubmenusOnClick'] &&
Expand Down

1 comment on commit 4b08cf1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 4b08cf1.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8750827416
📝 Reported issues:

Please sign in to comment.