0

I'm using the comments_template() function to echo out my comments template in the sidebar of each post. This is a requirement of the project.

It has been requested that I only echo out the three most recent comments in the sidebar for each post.

I've searched high and wide for a solution here and can't find anything.

Is there a function I can pop into my functions.php file to echo out only the most three recent comments for each post?

If it could include some jquery to add a "read more" link that opens up the rest of the comments that would be great, but it's not essential.

3 Answers 3

0

There is one more way to achieve this, without overriding files, using functions.php.
Add following code in functions.php file.

add_filter('wp_list_comments_args', 'override_args', 10, 1);

function override_args($args)
{
   $args = array(
                 'style'       => 'ol',
                 'short_ping'  => true,
                 'avatar_size' => 56,
                 'per_page' => '3'
                );
   return $args;
}
0

If you want to display only recent comments on posts without using a plugin, use the following code in your theme's template file:

<?php global $wpdb;

$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, 
comment_post_ID, comment_author, comment_date_gmt, comment_approved, 
comment_type,comment_author_url, 
SUBSTRING(comment_content,1,50) // NUMBER OF CHARACTERS
AS com_excerpt FROM $wpdb->comments 
LEFT OUTER JOIN $wpdb->posts 
ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) 
WHERE comment_approved = '1' 
AND comment_type = '' 
AND post_password = '' 
ORDER BY comment_date_gmt 
DESC LIMIT 3"; // NUMBER OF COMMENTS

$comments = $wpdb->get_results($sql);
$output   = $pre_HTML;
$output  .= "\n<ul>";

foreach ($comments as $comment) {
    $output .= "\n<li>"."<a href=\"" . get_permalink($comment->ID) . 
    "#comment-" . $comment->comment_ID . "\" title=\"on " . 
    $comment->post_title . "\">" .strip_tags($comment->comment_author) 
    .":<br/><div>" . strip_tags($comment->com_excerpt) 
    ."</div></a></li>";
}
$output .= "\n</ul>";
$output .= $post_HTML;

echo $output;
?>
0

Since you are using comments_template() function to display comments, it internally calls comments.php file of your theme.

In comments.php file, you should have a function wp_list_comments() with some parameters. You have to add one more parameter per_page in that function, as follows :

wp_list_comments( array(
                       'style'       => 'ol',
                       'short_ping'  => true,
                       'avatar_size' => 56,
                       'per_page' => '3'
                       )              
                );
3
  • Thanks for response. Unfortunately I can't use the method of overriding files in my child theme as it's a premium genesis child theme and will at some point in the future have an update applied to it. Is there any way I can achieve the above in the funtions.php file (which I'm using a plugin for)?
    – Connor
    Commented Nov 3, 2015 at 6:20
  • Please see second answer, and see if that works for you. Commented Nov 3, 2015 at 6:36
  • But both answers are correct. Thus I wrote answers differently. Commented Nov 3, 2015 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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