admin管理员组

文章数量:1202579

I'm using Wordpress 3.9.2, and having trouble with the wp_trim_words function. It seems to be cutting the string down to X characters instead of X words.

Example

Put the following in a template file:

<?php 
$str = "Imagine discovering a secret language spoken only online by a knowledgeable and learned few. Over a period of weeks, as you begin to tease out the meaning of this curious tongue and ponder its purpose, the language appears to shift in subtle but fantastic ways, remaking itself daily before your eyes. And just when you are poised to share your findings with the rest of the world, the entire thing vanishes.";

$str_excerpt = wp_trim_words($str, $num_words = 5, $more = '...' );

echo $str_excerpt;
?>

In my browser, I see the output as Imagi.... That's not how it should function though, as far as I read from the docs:

Description

This function trims text to a certain number of words and returns the trimmed text.

Usage

$trimmed = wp_trim_words( $text, $num_words = 55, $more = null);

Is there a setting or perhaps something in the theme I'm using that's causing this behavior?

I'm using Wordpress 3.9.2, and having trouble with the wp_trim_words function. It seems to be cutting the string down to X characters instead of X words.

Example

Put the following in a template file:

<?php 
$str = "Imagine discovering a secret language spoken only online by a knowledgeable and learned few. Over a period of weeks, as you begin to tease out the meaning of this curious tongue and ponder its purpose, the language appears to shift in subtle but fantastic ways, remaking itself daily before your eyes. And just when you are poised to share your findings with the rest of the world, the entire thing vanishes.";

$str_excerpt = wp_trim_words($str, $num_words = 5, $more = '...' );

echo $str_excerpt;
?>

In my browser, I see the output as Imagi.... That's not how it should function though, as far as I read from the docs:

Description

This function trims text to a certain number of words and returns the trimmed text.

Usage

$trimmed = wp_trim_words( $text, $num_words = 55, $more = null);

Is there a setting or perhaps something in the theme I'm using that's causing this behavior?

Share Improve this question asked Aug 19, 2014 at 18:42 BanjerBanjer 7601 gold badge6 silver badges11 bronze badges 3
  • Sounds like something has added a filter to set the word count method to characters. This is usually only done in the case of languages such as Chinese where each character is a word. What language are you using for the blog? – BA_Webimax Commented Aug 19, 2014 at 19:10
  • My wp-config.php file has define('WPLANG', '');, which means I'm defaulting to English, as desired. – Banjer Commented Aug 19, 2014 at 19:40
  • Do you have anything on the site that isn't English in any way? Anything that might have something to do with translations? – Otto Commented Aug 19, 2014 at 21:26
Add a comment  | 

1 Answer 1

Reset to default 2

I think you need to use mb_strimwidth function for trim the characters instead of words.

echo mb_strimwidth(get_the_title(), 0, 40, '...');

本文标签: excerptwptrimwords is trimming by character instead of by words