function trunc($phrase, $max_words, $after = null) {
$phrase_array = explode(\' \',$phrase);
if(count($phrase_array) > $max_words && $max_words > 0)
$phrase = implode(\' \',array_slice($phrase_array, 0, $max_words)) . $after;
return $phrase;
}
如果输入中的单词多于$max\\U单词,此函数将返回缩短的输入。您可以这样使用它:
$string = "This is a sample string";
echo trunc($string, 3);
这将呼应出“这是一个”
在你的情况下,你必须像
<?php $taxonomy = \'peoples\';$terms = get_the_terms( $post->ID , \'peoples\' ); if ( !empty( $terms ) ) : foreach ( $terms as $term ) {if($counter++ >= 1) break; $link = get_term_link( $term, $taxonomy ); if ( !is_wp_error( $link ) ) echo \'<h2>Profile: \' . $term->name . \'</h2><ul id="profile"><li class="big-listing \' . $term->slug. \'"><div class="text">\' .trunc($term->description, 40).\'</div></li></ul>\';} endif;?>
第三个参数用于在字符串缩短后使用。
$string = "This is a sample string";
echo trunc($string, 3, \'...\');
这将呼应出“这是一个…”