函数文档

excerpt_remove_blocks()

💡 云策文档标注

概述

excerpt_remove_blocks() 函数用于从内容字符串中解析块,并渲染适合摘要的块。它通过过滤允许的块类型,确保摘要仅包含文本相关的内容,避免无限循环。

关键要点

  • 函数检查内容是否包含块,若无则直接返回原内容。
  • 定义允许的内部块(如 core/paragraph、core/heading)和包装块(如 core/columns、core/group),并通过过滤器 excerpt_allowed_blocks 和 excerpt_allowed_wrapper_blocks 允许自定义。
  • 处理块时,跳过包含不允许或嵌套内部块的块,并渲染符合条件的块。

代码示例

function excerpt_remove_blocks( $content ) {
    if ( ! has_blocks( $content ) ) {
        return $content;
    }

    $allowed_inner_blocks = array(
        null,
        'core/freeform',
        'core/heading',
        'core/html',
        'core/list',
        'core/media-text',
        'core/paragraph',
        'core/preformatted',
        'core/pullquote',
        'core/quote',
        'core/table',
        'core/verse',
    );

    $allowed_wrapper_blocks = array(
        'core/columns',
        'core/column',
        'core/group',
    );

    $allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks );
    $allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks );
    $allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
    $blocks = parse_blocks( $content );
    $output = '';

    foreach ( $blocks as $block ) {
        if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
            if ( ! empty( $block['innerBlocks'] ) ) {
                if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) {
                    $output .= _excerpt_render_inner_blocks( $block, $allowed_blocks );
                    continue;
                }

                foreach ( $block['innerBlocks'] as $inner_block ) {
                    if (
                        ! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
                        ! empty( $inner_block['innerBlocks'] )
                    ) {
                        continue 2;
                    }
                }
            }

            $output .= render_block( $block );
        }
    }

    return $output;
}

注意事项

  • 动态块添加到允许列表时,必须避免生成另一个摘要,否则可能导致无限循环。
  • 函数依赖于 parse_blocks()、has_blocks() 和 render_block() 等核心函数。

📄 原文内容

Parses blocks out of a content string, and renders those appropriate for the excerpt.

Description

As the excerpt should be a small string of text relevant to the full post content, this function renders the blocks that are most likely to contain such text.

Parameters

$contentstringrequired
The content to parse.

Return

string The parsed and filtered content.

Source

function excerpt_remove_blocks( $content ) {
	if ( ! has_blocks( $content ) ) {
		return $content;
	}

	$allowed_inner_blocks = array(
		// Classic blocks have their blockName set to null.
		null,
		'core/freeform',
		'core/heading',
		'core/html',
		'core/list',
		'core/media-text',
		'core/paragraph',
		'core/preformatted',
		'core/pullquote',
		'core/quote',
		'core/table',
		'core/verse',
	);

	$allowed_wrapper_blocks = array(
		'core/columns',
		'core/column',
		'core/group',
	);

	/**
	 * Filters the list of blocks that can be used as wrapper blocks, allowing
	 * excerpts to be generated from the `innerBlocks` of these wrappers.
	 *
	 * @since 5.8.0
	 *
	 * @param string[] $allowed_wrapper_blocks The list of names of allowed wrapper blocks.
	 */
	$allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks );

	$allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks );

	/**
	 * Filters the list of blocks that can contribute to the excerpt.
	 *
	 * If a dynamic block is added to this list, it must not generate another
	 * excerpt, as this will cause an infinite loop to occur.
	 *
	 * @since 5.0.0
	 *
	 * @param string[] $allowed_blocks The list of names of allowed blocks.
	 */
	$allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
	$blocks         = parse_blocks( $content );
	$output         = '';

	foreach ( $blocks as $block ) {
		if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
			if ( ! empty( $block['innerBlocks'] ) ) {
				if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) {
					$output .= _excerpt_render_inner_blocks( $block, $allowed_blocks );
					continue;
				}

				// Skip the block if it has disallowed or nested inner blocks.
				foreach ( $block['innerBlocks'] as $inner_block ) {
					if (
						! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
						! empty( $inner_block['innerBlocks'] )
					) {
						continue 2;
					}
				}
			}

			$output .= render_block( $block );
		}
	}

	return $output;
}

Hooks

apply_filters( ‘excerpt_allowed_blocks’, string[] $allowed_blocks )

Filters the list of blocks that can contribute to the excerpt.

apply_filters( ‘excerpt_allowed_wrapper_blocks’, string[] $allowed_wrapper_blocks )

Filters the list of blocks that can be used as wrapper blocks, allowing excerpts to be generated from the innerBlocks of these wrappers.

Changelog

Version Description
5.0.0 Introduced.