Horje
Function to truncate comment text on home page. - Wordpress Solution
I am showing most recent comments on the home page using:
<?php $_comments=get_comments('number=5'); ?>
<?php foreach($_comments as $comm): ?>
<?php  echo $comm->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.'<a rel="nofollow" href="'.$comment_link.'">...</a><br />';
};


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



<?php $_comments=get_comments('number=5'); ?>

<?php foreach($_comments as $comm): ?>

<?php print mfields_shorten( $comm->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

<?php
function cut($str, $comment_link, $len = "65") {
	if(function_exists('mb_strlen')) {
		return mb_strlen($str,'UTF-8')<$len ? $str : (mb_substr($str,0,$len-1,'UTF-8').'<a rel="nofollow" href="$comment_link"...</a>');
	}
	if( function_exists('iconv_strlen') ) {
		return iconv_strlen($str,'UTF-8')<$len ? $str : (iconv_substr($str,0,$len-1,'UTF-8').'<a rel="nofollow" href="$comment_link"...</a>');
	}
	return strlen($str)<2*$len ? $str : (substr($str,0,2*$len-2).'<a rel="nofollow" href="$comment_link"...</a>');
}
$_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); 
?>





Wordpress

Related
pagination with custom query with permalinks in functions.php - Wordpress Solution pagination with custom query with permalinks in functions.php - Wordpress Solution
Title showing up in body of post - Wordpress Solution Title showing up in body of post - Wordpress Solution
WP Admin White Screen - Wordpress Solution WP Admin White Screen - Wordpress Solution
User Photo in Get Recent Comments Widget - Wordpress Solution User Photo in Get Recent Comments Widget - Wordpress Solution
Backend-Pages with Frontend-Themes (Again!) - Wordpress Solution Backend-Pages with Frontend-Themes (Again!) - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8