Forums

[resolved] Comment excerpts (5 posts)

  1. freespeed
    Member
    Posted 1 year ago #

    Hi, am having problems with displaying the 5 latest comments as excerpts on a home page. I have used the following code which displays the latest 5 comments nicely, but I have trawled the codex and google to find how to reduce the content of the comments to an excerpt. I know there is get_comment_excerpt but I am not sure how to include it in the following code.

    Also, I want to restrict the comments from the Feedback page to display (page id =119), is that possible?

    The site is here

    The code I am currently using in the sidebar is:

    <li class="recentcom">
    <h2>Latest Feedback</h2>
    <ul class="recent-comment">
    <?php
    $comments = get_comments('number=4');
      foreach($comments as $comm) :
    
      $url = '<a href="'. get_permalink($comm->comment_post_ID).'#comment-'.$comm->comment_ID .'" title="'.$comm->comment_author .' | '.get_the_title($comm->comment_post_ID).'">' . $comm->comment_author . '</a>';
    ?>
    <li>
    <a><?php echo $url; ?></a>
    <p><?php echo $comm->comment_content; ?></p>
    </li>
    <?php
      endforeach;
    ?>
    </ul>
    </li>

    Any help would be much appreciated.

  2. ekynoxe
    Member
    Posted 1 year ago #

    Hi freespeed, there is a function called comment_excerpt you can use to echo the comment excerpt of each comment. You can also use get_comment_excerpt if you don't want to echo it.

    It's all here: http://codex.wordpress.org/Function_Reference/comment_excerpt

    Your code would look something like:

    <li class="recentcom">
    <h2>Latest Feedback</h2>
    <ul class="recent-comment">
    <?php
    $comments = get_comments('number=4');
      foreach($comments as $comm) :
    
      $url = '<a>comment_post_ID).'#comment-'.$comm->comment_ID .'" title="'.$comm->comment_author .' | '.get_the_title($comm->comment_post_ID).'">' . $comm->comment_author . '</a>';
    ?>
    
    <li>
    <a><?php echo $url; ?></a>
    <p><?php comment_excerpt( $comm->comment_ID ); ?></p>
    </li>
    <?php
      endforeach;
    ?>

    I hope this helps

  3. ekynoxe
    Member
    Posted 1 year ago #

    I forgot to say, if you want to restrict the comments to page id 119, simply pass this id in the get_comments function parameters. Also, your code fetches the last 4, not 5 comments, see below:

    <?php
    $comments = get_comments('post_id=119&number=5');
    ...

    or

    <?php
    $comments = get_comments(array('post_id'=>119,'number'=>'5'));
    ...

    http://codex.wordpress.org/Function_Reference/get_comments

    regarding the get_comment_excerpt, the difference is that it does not echo the comment excerpt. In other words:

    <?php comment_excerpt( $comm->comment_ID ); ?>

    and

    <?php echo get_comment_excerpt( $comm->comment_ID ); ?>

    will do the same thing

  4. freespeed
    Member
    Posted 1 year ago #

    Thanks Ekynoxe, that was a great help! All solved now.

  5. alxvallejo
    Member
    Posted 6 months ago #

    Be sure to try both methods from above as only the get_comment_excerpt method worked on my script.

Topic Closed

This topic has been closed to new replies.

About this Topic