_excerpt_render_inner_blocks()
云策文档标注
概述
_excerpt_render_inner_blocks() 函数用于从允许的包装块中渲染内部块,以生成摘要。它递归处理块结构,仅渲染指定允许的内部块。
关键要点
- 函数接收两个参数:$parsed_block(解析后的块数组)和 $allowed_blocks(允许的内部块列表)。
- 通过遍历 $parsed_block['innerBlocks'],检查每个内部块的 blockName 是否在 $allowed_blocks 中,若不在则跳过。
- 如果内部块没有子块,则使用 render_block() 渲染;否则递归调用 _excerpt_render_inner_blocks() 处理嵌套块。
- 返回渲染后的内部块字符串,用于摘要生成。
- 自 WordPress 5.8.0 版本引入,位于 wp-includes/blocks.php 文件中。
代码示例
function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) {
$output = '';
foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) {
continue;
}
if ( empty( $inner_block['innerBlocks'] ) ) {
$output .= render_block( $inner_block );
} else {
$output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks );
}
}
return $output;
}注意事项
- 此函数主要用于摘要生成过程,确保只渲染允许的块类型,避免不必要的内容。
- 与 excerpt_remove_blocks() 和 _excerpt_render_inner_columns_blocks() 等相关函数配合使用,处理块编辑器内容。
- 递归调用可能影响性能,需注意块结构的深度和允许列表的大小。
原文内容
Renders inner blocks from the allowed wrapper blocks for generating an excerpt.
Parameters
$parsed_blockarrayrequired-
The parsed block.
$allowed_blocksarrayrequired-
The list of allowed inner blocks.
Source
function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) {
$output = '';
foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) {
continue;
}
if ( empty( $inner_block['innerBlocks'] ) ) {
$output .= render_block( $inner_block );
} else {
$output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks );
}
}
return $output;
}
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Introduced. |