Navigation block: Hiding a link item breaks the HTML into two separate lists
Description
I hid one of the links inside the navigation block, and on the frontend it shows two <ul> elements, one with all menu items up to the hidden link, and the next with all items after the hidden link. This affects styling.
I noticed it on our site because our menu uses CSS multi-column layout, and the menu items after the hidden item were below on their own line instead of in the usual flow.
Step-by-step reproduction instructions
- Add navigation block.
- Add several items to navigation block.
- Use the new block visibility feature to hide one of the items.
- View frontend and see menu has two
<ul>s now.
Screenshots, screen recording, code snippet
No response
Environment info
- WordPress 7.0
Please confirm that you have searched existing issues in the repo.
- [x] Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
- [x] Yes
Please confirm which theme type you used for testing.
- [x] Block
- [ ] Classic
- [ ] Hybrid (e.g. classic with theme.json)
- [ ] Not sure
I can reproduce this on WordPress 7.0 with Gutenberg active.
Hiding a middle navigation item results in two separate <ul class="wp-block-navigation__container">
elements in the DOM one containing items before the hidden block,
one containing items after it.
Looking into the Navigation block's render logic for a fix.
Root cause identified:
In WP_Navigation_Block_Renderer::get_inner_blocks_html(), the loop tracks list state using an $is_list_open flag. When a block is hidden via block visibility, $inner_block->render() returns an empty string ''. WP_HTML_Tag_Processor finds no <li> in the empty string, so $is_list_item is false. This satisfies the close condition ! $is_list_item && $is_list_open, closing the <ul> prematurely. The next visible item then opens a brand new <ul>, producing two separate lists.
Fix: Added an early continue to skip empty block markup inside the foreach loop, preserving the $is_list_open state across hidden blocks:
if ( '' === $inner_block_markup ) {
continue;
}
PR attached in #78793.