Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$10
Function to truncate comment text on home page.
<?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.
This question has been answered.
badnews | 06/10/10 at 12:34am
Edit
(6) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
06/10/10
1:32amRashad Aliyev says: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. : http://www.wplancer.com/how-to-limit-content-in-wordpress/Previous versions of this answer: 06/10/10 at 1:32am
-

Last edited:
06/10/10
12:39amMichael Fields says:
<?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;
}
Previous versions of this answer: 06/10/10 at 12:39am | 06/10/10 at 12:39am
- 06/10/10 12:51am
badnews says:Hey Micheal,
This works great... Is it possible to have the "..." link to the actual comment without much work? :) - 06/10/10 12:58am
Michael Fields says: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;
} - 06/10/10 12:59am
Michael Fields says: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;
}
- 06/10/10 1:02am
badnews says: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 - 06/10/10 1:05am
badnews says:Great no more errors but this links to http://site.com/comment-page-#comment-, which gets me a 404.
- 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?
- 06/10/10 1:09am
badnews says: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?
Yes, it got me a link to "http://site.com/comment-page-#comment-" but that gets me a 404. - 06/10/10 1:16am
Michael Fields says: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;
}
- 06/10/10 1:22am
badnews says:No this gives out the same error message.
- 06/10/10 1:39am
Michael Fields says:Please post your comments code to pastebin or email to michael {{AT} } mfields [ [D0T]] org and I'll give it a look. It's hard to code "in the dark" :)
- 06/10/10 11:42pm
badnews says:Did you have a chance to go through it again?
- 06/10/10 12:51am
-

Last edited:
06/10/10
12:39amChris Lee says:<?php $_comments=get_comments('number=5'); ?>
<?php foreach($_comments as $comm): ?>
<?php $commentshort = substr($comm->comment_content, 0, 69); ?>- 06/10/10 11:45pm
badnews says:Chris, this just gets rid of the whole text. I would like to truncate it after a certain amount of characters.
- 06/10/10 11:45pm
-

Last edited:
06/10/10
12:44amMonster Coder says: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
-

Last edited:
06/10/10
2:27amSvilen Popov says:<?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);
?>Previous versions of this answer: 06/10/10 at 2:27am
- 06/10/10 11:42pm
badnews says:Hey Svilen,
Thanks but this doesn't work. - 06/11/10 12:09am
Svilen Popov says: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;
?>
- 06/10/10 11:42pm
-

Last edited:
06/14/10
12:29amDarrin Boutote says: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 />';
};Previous versions of this answer: 06/12/10 at 9:18am
- 06/14/10 12:33am
badnews says:Thanks!
- 06/14/10 12:33am
This question has expired.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
