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 |1 Answer
Reset to default 2I 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
版权声明:本文标题:excerpt - wp_trim_words is trimming by character instead of by words 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738620864a2103181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp-config.php
file hasdefine('WPLANG', '');
, which means I'm defaulting to English, as desired. – Banjer Commented Aug 19, 2014 at 19:40