I am showing most recent comments on the home page using:
comment_content; ?>
I need some way to limit, truncate the comment content after a certain (60-70) amount of characters.
Solution - 1
Try this:
$_comments = get_comments('number=5');
foreach($_comments as $comm) {
$comment_link = get_comment_link($comm->comment_ID);
$comment_content = strip_tags($comm->comment_content);
$comment_content = substr($comment_content, 0, 60 );
echo $comment_content.'... ';
};
Solution - 2
I made some test in my locale.
That's very useful links made a limited content. And also you can develop it for your comments. : [[LINK href="http://www.wplancer.com/how-to-limit-content-in-wordpress/"]]http://www.wplancer.com/how-to-limit-content-in-wordpress/[[/LINK]]
Solution - 3
comment_content, 60 ); ?>
function mfields_shorten( $string, $crop = 23, $trail = '...') {
$string = strip_tags( $string );
$string = trim( $string );
$string = html_entity_decode( $string, ENT_QUOTES, 'UTF-8' );
$string = rtrim( $string, '-' );
$crop = intval( $crop );
return ( strlen( $string ) > $crop ) ? substr_replace( $string, $trail, $crop ) : $string;
}
Solution - 4
comment_content, 0, 69); ?>
Solution - 5
if you need a solution for non-English too that won't break the meaning you may try this one:
http://stackoverflow.com/questions/2154220/truncate-a-multibyte-string-to-n-chars
Solution - 6
');
}
if( function_exists('iconv_strlen') ) {
return iconv_strlen($str,'UTF-8')<$len ? $str : (iconv_substr($str,0,$len-1,'UTF-8').'');
}
return strlen($str)<2*$len ? $str : (substr($str,0,2*$len-2).'');
}
$_comments=get_comments('number=5');
foreach($_comments as $comm):
$comm_url = get_permalink($comm->comment_post_ID). "#comment-". $comm->comment_ID;
echo cut(comm->comment_content, $comm_url);
?>
|