WP_TRIM_WORS按字符而不是按单词进行裁剪

时间:2014-08-19 作者:Banjer

我正在使用Wordpress 3.9.2,在wp_trim_words 作用它似乎将字符串缩减为X个字符,而不是X个单词。

Example

将以下内容放入模板文件中:

<?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;
?>
在我的浏览器中,我看到的输出为Imagi.... 但据我所知,这不是它应该起的作用the docs:

描述

此函数用于将文本修剪为一定数量的单词,并返回修剪后的文本。

用法

$trimmed=wp\\u trim\\u words($text,$num\\u words=55,$more=null);

我使用的主题中是否有导致这种行为的设置或其他原因?

1 个回复
SO网友:Syed Figar Ali Jaffri

我想你需要使用mb_strimwidth 用于修剪字符而不是单词的函数。

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

结束

相关推荐

get post excerpt by query

我正在尝试通过查询获取帖子摘要我的代码如下global $wpdb; $posts = $wpdb->get_results (\"SELECT post_title, post_excerpt FROM $wpdb->posts WHERE post_status = \'publish\' AND (post_type = \'post\') AND post_content LIKE \'%My Text%\' ORDER BY post_date DESC\