Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am calling recent comments into my template with

$comment->post_title  

the variable length is specified like this :

SUBSTRING(comment_content,1,180)

how could I write a conditional statement just for comments that exceed the limits of the vairable >180 ?

I was thinking It would be good to have a ... for longer comments and a more link.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

By using a function to perform that:

recent_comment_text_more($comment_content)

that function would look like (in case you're using PHP, part of your code looks from another language):

function recent_comment_text_more($comment_content, $more_href)  {
    if (strlen($comment_content) > 180) {
        $comment_content = substr($comment_content, 0, 177) . sprintf('<a href="%s">... (more)</a>', $more_href);
    } 
    return $comment_content;
}

Good luck!

Multibyte charset safe variant

As pointed out in a comment, e.g. for UTF-8, see mb_internal_encoding() for specifying the encoding to use:

function recent_comment_text_more($comment_content, $more_href)  {
    if (mb_strlen($comment_content) > 180) {
        $comment_content = mb_substr($comment_content, 0, 177) . sprintf('<a href="%s">... (more)</a>', $more_href);
    } 
    return $comment_content;
}
share|improve this answer
2  
Use mb_substr() or multibyte characters may break. –  toscho Mar 8 '11 at 6:47
    
I added a quick edit; the shortened content was being stored in $comment_conent, which would have resulted in this not working correctly. –  John P Bloch Apr 8 '11 at 20:24
    
Thanks for taking care. –  hakre Apr 9 '11 at 10:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.