当前浏览:首页 / WordPress钩子 / the_content

apply_filters( 'the_content', string $content )

过滤文章内容

content

themore...


参数

$content

(string) 当前文章的内容


说明

此过滤器用于在从数据库检索文章后,以及在将其打印到屏幕之前过滤文章的内容。

用法

使用此过滤器时,重要的是检查是否使用条件is_main_query()in_the_loop()过滤主查询中的内容。主post查询可以被视为主post循环,显示post、页面或归档的主要内容。如果没有这些条件,您可能会无意中在边栏、页脚或其他地方过滤自定义循环的内容。

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 single Post.
    if ( is_singular() && in_the_loop() && is_main_query() ) {
        return $content . esc_html__( 'I’m filtering the content inside the main loop', 'wporg');
    }

    return $content;
}


源码

查看源码 官方文档


更新日志

版本描述
0.71开始引入

使用示例

  • 示例1

    通过在add_filter中设置优先级,可以选择在执行the_content函数之前还是之后处理$content的短码。默认值(10)将在过滤器函数末尾返回$content后处理短代码。在传递过滤函数$content之前,一个较大的数字(如99)已经处理了短代码。

  • 示例2

    请注意,调用get_the_content()函数时没有使用the_content过滤器。

  • 示例3

    示例

    /* Add a paragraph only to Pages. */
    function my_added_page_content ( $content ) {
        if ( is_page() ) {
            return $content . '<p>Your content added to all pages (not posts).</p>';
        }
    
        return $content;
    }
    add_filter( 'the_content', 'my_added_page_content');
    
  • 示例4

    合并多个字符串替换是过滤器the_content的常见用途,现在使用PHP7可以使用preg_replace_callback_array();合并多个回调函数(如果有必要)。

    示例:

    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([
    			// 1. Removes WordPress injected <p> tags surrounding images in post content.
    			'/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU' => function ( &$matches ) {
    				return $matches[1] . $matches[2] . $matches[3];
    			},
    			// 2. Adds custom data-attribute to <p> tags providing a paragraph id number.
    			'|<p>|' => function ( &$matches ) {
    				static $i = 1;
    				return sprintf( '<p data-p-id="%d">', $i++ );
    			},
    		], $content );
    	}
    	return $content;
    }
    
  • 示例5

    使用示例具有误导性。除非你对页面不感兴趣,否则应该

    if ( ( is_single() || is_page() ) && in_the_loop() && is_main_query() ) {
    ...
    }

    您还应该为过滤器添加足够高的优先级,以便在渲染之前挂接到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;
    }

    这应该包含在functions.php中,我花了半天的时间才弄明白为什么我的过滤器什么都没用。

  • 示例6

    如果您想显示插件中的自定义字段(包括ACF字段),这也是一个很好的挂钩。

    下面的例子来自杰夫·斯塔尔的插件课程。

    // display all custom fields for each post
    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;
    }
    add_filter( 'the_content', 'wpdocs_display_all_custom_fields' );
    
  • 示例7

    使用示例将类添加到p标签和h2标签。

    <?php
    function wpdocs_replace_content( $text_content ) {
    	if ( is_page() ) {
    		$text = array(
    			'<p>' => '<p class="text-danger">',
    			'<h2>' => '<h2 class="h2">',
    		);
    
    		$text_content = str_ireplace( array_keys( $text ), $text, $text_content );
    	}
    
    	return $text_content;
    }
    add_filter( 'the_content', 'wpdocs_replace_content' );
    ?>
    
  • 示例8

    一个有用的钩子,可以从你的网站中过滤出粗俗的词语,并为你的网站访问者保持整洁。

    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;
    }
    
  • 示例9

    使用以下示例验证,如果插入到内容中的任何图像的ALT属性为空,则获取文件名。

    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;
    	}
    }
    add_filter( 'the_content', 'wpdocs_replaceALT' );