当前浏览:首页 / WordPress函数 / wp_trim_words()

wp_trim_words( string $text, int $num_words = 55, string $more = null )

将文本修剪为一定数量的单词

trim


描述

此函数是本地化的。对于按单个字符计算“单词”的语言(如东亚语言),参数$num_words将应用于单个字符的数量。


参数

$text

(string)(必填) 要修剪的文本。

$num_words

(int)(可选) 字数。

默认值: 55

$more

(string)(可选) 如果$text需要修剪,则追加什么。默认值为'…'。

默认值: null


返回

(string) 修剪文本。



源码

查看源码 官方文档


更新日志

版本描述
3.3.0开始引入

使用示例

  • 示例1

    剥离格式的示例:

    <?php
    echo wp_trim_words( get_the_content(), 40, '...' );
    ?>
    
    
  • 示例2

    此函数用于修剪溢出文本。我曾经在较小的屏幕上以固定高度的div显示经过修剪的标题。这只是一行代码:)

    <?php echo wp_trim_words( get_the_title(), 15 ); ?>
  • 示例3

    注意:wp_trim_words()适用于任何文本字符串:

    $text = 'Some very long text';
    $words = 20;
    $more = ' […]';
    
    $excerpt = wp_trim_words( $text, $words, $more );
    
  • 示例4

    如果出于某种原因,您需要修剪的单词,这里是此函数的修改版本,用于返回数组中前后的单词:

    if( ! function_exists( 'smyles_wp_split_words' ) ){
    	/**
    	 * Split a string based on word count
    	 *
    	 * This is similar to WordPress wp_trim_words function, but instead of just trimming after a certain amount of
    	 * words, this function returns an array with 'before' and 'after' keys -- providing you the string of text up
    	 * to the number of words (in before key), and the words after (in the after key).  After key will be empty string
    	 * if there are less words in the passed string than number of words to split on.
    	 *
    	 *
    	 * @param string    $text
    	 * @param int       $num_words
    	 *
    	 * @return array    Array with `before` and `after` keys. The `before` key contains all words up to $num_words, the
    	 *                  `after` key contains the words after $num_words (or empty string if passed string has less words
    	 *                  than passed in $text)
    	 *
    	 */
    	function smyles_wp_split_words( $text, $num_words = 55 ) {
    		$text = wp_strip_all_tags( $text );
    		/*
    		 * translators: If your word count is based on single characters (e.g. East Asian characters),
    		 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
    		 * Do not translate into your own language.
    		 */
    		if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
    			$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
    			preg_match_all( '/./u', $text, $words_array_matches );
    			$words_array = $words_array_matches[0];
    			$sep         = '';
    		} else {
    			$words_array = preg_split( "/[\n\r\t ]+/", $text, -1, PREG_SPLIT_NO_EMPTY );
    			$sep         = ' ';
    		}
    		if ( count( $words_array ) > $num_words ) {
    			$before = implode( $sep, array_slice( $words_array, 0, $num_words ) );
    			$after  = implode( $sep, array_slice( $words_array, $num_words, count( $words_array ) - 1 ) );
    		} else {
    			$before = implode( $sep, $words_array );
    		}
    		$results = array(
    			'before' => $before,
    			'after' => isset( $after ) ? $after : ''
    		);
    		return $results;
    	}
    }
    
  • 示例5

    示例:在评论自定义查询中显示评论摘录。

    $args = array(
    	'number' => 3,
    );
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );
    if ( $comments ) {
    	foreach ( $comments as $comment ) {
    		echo wp_trim_words( $comment->comment_content, 20, ' […]' );
    	}
    }