• 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)
  • Moderator t-p

    (@t-p)

    Thread Starter rodrego

    (@rodrego)

    Hi t-p,

    I couldn’t see how one relates to the other… All I need is that the post comment section shows only X number of recent comments, instead of all of them.

    Is this possible?

    sure…by upgrading to use pagination! ;/

    in all seriousness, you’ve gotta do it sooner or later. wp 2.6 is out of date, and insecure. It has well publicized security issues.

    If you continue to put off upgrading because it may be hard, and you get hacked and lose everything, that would kinda suck!

    Plus if you are on a shared server, you are putting everyone else on your server at risk.

    Besides, you are missing out on a lot of cool new features!

    I know that doesn’t address your question directly, just offering some insight

    Thread Starter rodrego

    (@rodrego)

    Hi Rev. Voodoo,

    I designed the website, but I’m not responsible for its maintenance. I’m doing this small change as a favor to the site owner, but it’s up to her to upgrade it.

    Anyway, maybe I will have to upgrade if I can’t find an answer for this. 🙁

    Don’t you have any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Displaying recent comments from only one post’ is closed to new replies.