Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Function to truncate comment text on home page. WordPress

  • SOLVED

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.

Answers (6)

2010-06-12

Darrin Boutote answers:

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 href="'.$comment_link.'">...</a><br />';
};


badnews comments:

Thanks!

2010-06-10

Rashad Aliyev answers:

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]]

2010-06-10

Michael Fields answers:



<?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;
}


badnews comments:

Hey Micheal,

This works great... Is it possible to have the "..." link to the actual comment without much work? :)


Michael Fields comments:

Something like this might work for ya:

function mfields_shorten( $string, $crop = 23, $trail = '...') {
global $comment;
$string = strip_tags( $string );
$string = trim( $string );
$string = html_entity_decode( $string, ENT_QUOTES, 'UTF-8' );
$string = rtrim( $string, '-' );
$crop = intval( $crop );
$trail = '<a href="' . get_comment_link( $comment ) . '">' . $trail . '</a>'
return ( strlen( $string ) > $crop ) ? substr_replace( $string, $trail, $crop ) : $string;
}


Michael Fields comments:

Actually, there was an error there...

function mfields_shorten( $string, $crop = 23, $trail = '...') {
global $comment;
$string = strip_tags( $string );
$string = trim( $string );
$string = html_entity_decode( $string, ENT_QUOTES, 'UTF-8' );
$string = rtrim( $string, '-' );
$crop = intval( $crop );
$trail = '<a href="' . get_comment_link( $comment ) . '">' . $trail . '</a>';
return ( strlen( $string ) > $crop ) ? substr_replace( $string, $trail, $crop ) : $string;
}


badnews comments:

Sorry for mistyping your name earlier!

I get:

Parse error: syntax error, unexpected T_RETURN in /home/site/public_html/site/wp-content/themes/theme/functions.php on line 17


badnews comments:

Great no more errors but this links to http://site.com/comment-page-#comment-, which gets me a 404.


Michael Fields comments:

No worries, I would misspell it too if I weren't born with it :) I posted too soon! Did you try the 06/10/10 12:59am snippet?


badnews comments:

<blockquote>06/10/10 1:06am

Michael Fields says:

No worries, I would misspell it too if I weren't born with it :) I posted too soon! Did you try the 06/10/10 12:59am snippet?</blockquote>

Yes, it got me a link to "http://site.com/comment-page-#comment-" but that gets me a 404.


Michael Fields comments:

Does this help any?

function mfields_shorten( $string, $crop = 23, $trail = '...') {
global $comment;
$string = strip_tags( $string );
$string = trim( $string );
$string = html_entity_decode( $string, ENT_QUOTES, 'UTF-8' );
$string = rtrim( $string, '-' );
$crop = intval( $crop );
$trail = '<a href="' . get_comment_link( $comment->comment_ID ) . '">' . $trail . '</a>';
return ( strlen( $string ) > $crop ) ? substr_replace( $string, $trail, $crop ) : $string;
}


badnews comments:

No this gives out the same error message.


Michael Fields comments:

Please post your comments code to [[LINK href="http://wordpress.pastebin.com/"]]pastebin[[/LINK]] or email to michael {{AT} } mfields [ [D0T]] org and I'll give it a look. It's hard to code "in the dark" :)


badnews comments:

Did you have a chance to go through it again?

2010-06-10

Chris Lee answers:

<?php $_comments=get_comments('number=5'); ?>
<?php foreach($_comments as $comm): ?>
<?php $commentshort = substr($comm->comment_content, 0, 69); ?>


badnews comments:

Chris, this just gets rid of the whole text. I would like to truncate it after a certain amount of characters.

2010-06-10

Monster Coder answers:

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

2010-06-10

Svilen Popov answers:

<?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 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 href="$comment_link"...</a>');
}
return strlen($str)<2*$len ? $str : (substr($str,0,2*$len-2).'<a 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);
?>


badnews comments:

Hey Svilen,

Thanks but this doesn't work.


Svilen Popov comments:

Here is the clean code:
<?php
function cut_comment($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 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 href="'.$comment_link.'">...</a>');
}
return strlen($str)<2*$len ? $str : (substr($str,0,2*$len-2).'<a href="'.$comment_link.'">...</a>');
}
$_comments=get_comments('number=5');
foreach($_comments as $comm):
$comment_url = get_comment_link($comm->comment_ID );
echo cut_comment($comm->comment_content, $comment_url, 70);
endforeach;
?>