函数文档

excerpt_remove_footnotes()

💡 云策文档标注

概述

excerpt_remove_footnotes() 函数用于从内容字符串中解析并移除脚注标记,以生成适合摘要的文本。它检查内容是否包含特定脚注标记,并执行正则表达式替换来清理内容。

关键要点

  • 函数接受一个必需参数 $content,表示要解析的内容字符串,并返回处理后的字符串。
  • 如果内容中不包含 'data-fn=' 标记,函数直接返回原内容,避免不必要的处理。
  • 使用 preg_replace() 函数,通过正则表达式模式 '_s*d+s*_' 移除脚注相关的数字和空白字符。
  • 此函数在 WordPress 6.3.0 版本中引入,与 wp_trim_excerpt() 函数相关,用于生成内容摘要。

代码示例

function excerpt_remove_footnotes( $content ) {
    if ( ! str_contains( $content, 'data-fn=' ) ) {
        return $content;
    }

    return preg_replace(
        '_s*d+s*_',
        '',
        $content
    );
}

📄 原文内容

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

Parameters

$contentstringrequired
The content to parse.

Return

string The parsed and filtered content.

Source

function excerpt_remove_footnotes( $content ) {
	if ( ! str_contains( $content, 'data-fn=' ) ) {
		return $content;
	}

	return preg_replace(
		'_<sup data-fn="[^"]+" class="[^"]+">s*<a href="[^"]+" id="[^"]+">d+</a>s*</sup>_',
		'',
		$content
	);
}

Changelog

Version Description
6.3.0 Introduced.