将以下两个函数写进主题所附带的functions.php文件中
function LimitChar($max_char = 200, $more_text = '...', $more_link_text = '', $limit_type = 'content') {
if ($limit_type == 'title') {
$limiter = get_the_title();
} else {
$limiter = get_the_content();
}
$limiter = apply_filters('the_content', $limiter);
$limiter = strip_tags(str_replace(']]>', ']]>', $limiter));
if (strlen($limiter) > $max_char) {
$limiter = substr($limiter, 0, $max_char+1);
$limiter = utf8Conver($limiter);
echo $limiter;
echo $more_text;
if ($more_link_text != '') {
echo ' <a href="'; echo the_permalink(); echo '">'.$more_link_text.'</a>';
}
} else {
echo $limiter;
}
}
function utf8Conver($str) {
$len = strlen($str);
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ' '.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}
把模板文件中比如index.php中将截断函数替换成新的。
<?php LimitChar(200); ?>