Forums

recent comment question (4 posts)

  1. t-p
    Member
    Posted 2 years ago #

    Hi,

    I added this to my default sidebar to display recent comments. Works great!

    Question: How can I limit the characters to say 25 (Total: comment author + title):

    <?php
    $number=5; // number of recent comments desired
    $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
    ?>
    <ul id="recentcomments">
    <h2>Recent Comments</h2>
    <?php
    if ( $comments ) : foreach ( (array) $comments as $comment) :
    echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
    endforeach; endif;?></ul>

    Your guidance will greatly be appreciated. Thanks.

  2. peterjharrison
    Member
    Posted 2 years ago #

    Hi,

    You can do this by changing the following query:

    $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");

    With:

    $comments = $wpdb->get_results("SELECT SUBSTR(comment_content,0,25) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");

    Regards
    Peter

  3. t-p
    Member
    Posted 2 years ago #

    Thanks peterjharrison for your help,

    1. It did not limit total chars to 25
    2. Plus, now, instead of displaying recent comments, it shows older comments.

    Any other suggestion?

  4. t-p
    Member
    Posted 2 years ago #

    I still need guidance. anybody, please?

Topic Closed

This topic has been closed to new replies.

About this Topic