Displaying recent comments from only one post
-
I’m trying to display only the 20 most recent comments of each post in the comment section.
I can’t use comment pagination, as I’m working with an old version of WordPress and have already tweaked a lot the theme, so updating it could be risky.
So far all I have this code int the functions.php file, that displays the 20 most recent comments from the whole blog:
< ?php // Get Recet Comments Function function dp_recent_comments($no_comments = 10, $comment_len = 35) { global $wpdb; $request = "SELECT * FROM $wpdb->comments"; $request .= " JOIN $wpdb->posts ON ID = comment_post_ID"; $request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''"; $request .= " ORDER BY comment_date DESC LIMIT $no_comments"; $comments = $wpdb->get_results($request); if ($comments) { foreach ($comments as $comment) { ob_start(); ?> <li> <a href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>">< ?php echo dp_get_author($comment); ?>:</a> < ?php echo strip_tags(substr(apply_filters('get_comment_text', $comment->comment_content), 0, $comment_len)); ?> </li> < ?php ob_end_flush(); } } else { echo '<li>'.__('No comments', 'banago').''; } } function dp_get_author($comment) { $author = ""; if ( empty($comment->comment_author) ) $author = __('Anonymous', 'banago'); else $author = $comment->comment_author; return $author; }?>
Can you please help me turn it into a post-aware code, so that it displays only the 20 most recent comments from one post? Thanks in advance!
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Displaying recent comments from only one post’ is closed to new replies.