apply_filters( ‘the_content’, string $content )
过滤帖子内容。
参数
$content string
当前帖子的内容。
更多信息
此过滤器用于在从数据库检索帖子之后、将其打印到屏幕之前过滤帖子内容。
用法
使用此过滤器时,务必检查是否使用条件 is_main_query() 和 in_the_loop() 过滤主查询中的内容。主帖子查询可视为显示帖子、页面或存档主要内容的主要帖子循环。如果没有这些条件,您可能会无意中过滤侧边栏、页脚或其他地方的自定义循环内容。
注意:调用 get_the_content() 函数时不使用 the_content 过滤器。
您可以通过在 add_filter 中设置优先级来选择在执行 the_content 函数之前还是之后处理 $content 短代码。默认值 (10) 将在 Filter 函数末尾返回 $content 后处理短代码。较大的数字(例如 99)将在 Filter 函数传递 $content 之前已经处理了短代码。
示例
/* Add a paragraph only to Pages. */
add_filter( 'the_content', 'my_added_page_content');
function my_added_page_content ( $content ) {
if ( is_page() ) {
return $content . 'Your content added to all pages (not posts).
';
}
return $content;
}
整合多个字符串替换过滤器
字符串替换是过滤 the_content 的常见原因,使用 PHP7,您现在可以使用 preg_replace_callback_array(); 整合多个回调函数(如果有意义的话)。
// 1. Removes WordPress injectedtags surrounding images in post content. // 2. Adds custom data-attribute to
tags providing a paragraph id number. add_filter( 'the_content', 'multiple_string_replacements'); function multiple_string_replacements ( $content ) { if ( is_single() && in_the_loop() && is_main_query() ) { return preg_replace_callback_array([ '/
如果您想显示插件中的自定义字段(包括 ACF 字段),这也是一个很好的钩子。
// 显示所有文章的自定义字段
add_filter( 'the_content', 'wpdocs_display_all_custom_fields' );
function wpdocs_display_all_custom_fields( $content ) {
$custom_fields = 'Custom Fields';
$all_custom_fields = get_post_custom();
foreach ( $all_custom_fields as $key => $array ) {
foreach ( $array as $value ) {
if ( '_' !== substr( $key, 0, 1 ) ) {
$custom_fields .= ''. $key .' => '. $value .'';
}
}
}
return $content . $custom_fields;
}
使用示例向 p 标签和 h2 标签添加类。
add_filter( 'the_content', 'wpdocs_replace_content' );
function wpdocs_replace_content( $text_content ) {
if ( is_page() ) {
$text = array( '' => '
', '
' => '' );
$text_content = str_ireplace( array_keys( $text ), $text, $text_content );
}
return $text_content;
}
一个有用的钩子,可以过滤掉你网站上的粗俗词语,为你的站点访问者保持网站整洁。
add_filter( 'the_content', 'wpdocs_filter_vulgar_words' );
function wpdocs_filter_vulgar_words( $content ) {
$vulgar_words = array( $vulgar_word1, $vulgar_word2, $vulgar_word3, $vulgar_word4 );
foreach ( $vulgar_words as $word ) {
$hashed_word = substr( $word, 0, 1 ) . str_repeat( '*', strlen( $word ) - 1 );
$content = str_replace( $word, $hashed_word, $content );
}
return $content;
}
您还应该为过滤器添加足够高的优先级,以便在渲染发生之前挂接 the_content:
add_filter( 'the_content', 'filter_the_content_in_the_main_loop', -1 );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a post or page.
if ( ( is_single() || is_page() ) && in_the_loop() && is_main_query() ) {
return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
}
return $content;
}
使用下面的示例验证如果内容中插入的任何图像的 ALT 属性为空,则获取文件名。
add_filter( 'the_content', 'wpdocs_replaceALT' );
function wpdocs_replaceALT( $content ) {
if ( is_single() && in_the_loop() && is_main_query() ) {
libxml_use_internal_errors( true );
$post = new DOMDocument();
$post->loadHTML( $content );
$images = $post->getElementsByTagName( 'img' );
foreach ( $images as $image ) {
if ( empty( $image->getAttribute( 'alt' ) ) ) {
$src = $image->getAttribute( 'src' );
$alt = pathinfo( $src, PATHINFO_FILENAME );
$image->setAttribute( 'alt', $alt );
}
}
$content = $post->saveHTML();
return $content;
}
}
)\s*(<\/a>)?\s*<\/p>/iU' => function ( &$matches ) {
return $matches[1] . $matches[2] . $matches[3];
}, '|