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

Post Template: Ensure layout classnames are not attached to inner li elements #41827

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,29 @@ function render_block_core_post_template( $attributes, $content, $block ) {
$content = '';
while ( $query->have_posts() ) {
$query->the_post();

// Get an instance of the current Post Template block.
$block_instance = $block->parsed_block;

// Set the block name to one that does not correspond to an existing registered block.
// This ensures that for the inner instances of the Post Template block, we do not render any block supports.
$block_instance['blockName'] = 'core/null';
Copy link
Member

Choose a reason for hiding this comment

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

What would be the option for developers who would want block supports for core/post-template later?

Render them somewhere in the render callback?

Copy link
Contributor Author

@andrewserong andrewserong Jun 23, 2022

Choose a reason for hiding this comment

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

The supports still get rendered on the outer wrapper (this function is called with core/post-template) so the block supports are should still apply there (I think!).

I'm not too sure about how we'd handle styling for the inner wrapper to be honest — I think the best bet for pattern design would be to have a Group block as the first child of the inner wrapper, and then folks can use block supports there. I was almost wondering if we could use a group block instead of core/null, but that felt like going down a rabbithole that might be a bit broader in scope than solving the current bug 🤔

Longer-term, I'd love for us to figure out how to reliably get block spacing and block supports working nicely for styling up different kinds of post template layouts, though!


// Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
// `render_callback` and ensure that no wrapper markup is included.
$block_content = (
new WP_Block(
$block->parsed_block,
$block_instance,
array(
'postType' => get_post_type(),
'postId' => get_the_ID(),
)
)
)->render( array( 'dynamic' => false ) );
$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
$content .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';

// Wrap the render inner blocks in a `li` element with the appropriate post classes.
$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
$content .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
}

wp_reset_postdata();
Expand Down