Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to make a custum 'Recent Comments' code for use in the sidebar. I have come a long way, but there is just one thing that doesn't do what I'd like it to do.

What I want is that, per page with comments that is found, only the last comment is shown. I was hoping post_per_page would do the trick, but it doesn't!

This is my code:

<?php

$args=array(
'author_email' => '',
'ID' => '',
'karma' => '',
'number' => 5,
'offset' => '',
'orderby' => 'comment_date_gmt',
'order' => 'DESC',
'parent' => '',
'post_id' => '',
'post_author' => '',
'post_name' => '',
'post_parent' => '0',
'post_status' => 'publish',
'post_type' => 'page',
'posts_per_page' => 1,
'status' => 'approve',
'type' => '',
'user_id' => '',
'search' => '',
'count' => false
);
$comments = get_comments($args);

foreach($comments as $comment) :

?>
<div style="border: 0px solid #000000; height: 1%; overflow: hidden;">
<div style="border: 0px solid #000000; float: left; width: 28%;">
<a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo get_avatar(     $comment, 48 ); ?></a>
</div>
<div style="border: 0px solid #000000; float: right; width: 72%;">
<?php
$aantalcomments = get_comments_number( $comment->comment_post_ID );

?>  
<a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo($comment->comment_author) ?></a> op:<br>


<a href="<?php echo get_comment_link($comment->comment_ID); ?>">

<?php
echo($comment->post_name . " (" . $aantalcomments . ")<br>");
?>
</a>
<?php

echo get_comment_date(' l, j F, Y ', $comment->comment_ID);

?>
</div>
</div>
<hr>
<?php



endforeach;

?>
share|improve this question
    
What does happen? –  s_ha_dum Jan 12 '13 at 14:57
add comment

2 Answers

First you should KNOW all different posts with comments:

global $wpdb;

$SelectSQL = "SELECT DISTINCT `comment_post_ID` FROM {$wpdb->comments} LIMIT 0, 15";
$Rows      = $wpdb->get_results($SelectSQL);

then for each of these posts get the latest comment:

foreach($Rows as $row):
  $comment = get_comments(array('post_id'=>$row->comment_post_ID, 'number'=>1, 'status'=>'appprove'));
  $comment = $comment[0];

...

and continue with the display of comment, hope you are good at PHP ;) .

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

share|improve this answer
    
Well, I couldn't figure it out, but my brother, who IS REALLY GOOD in PHP did figure it out. He himself doesn't consider this the best of solution, but hey, it works. Maybe a quick-fix of which he isn't proud, but I am saved! ;-) –  AWK Jan 13 '13 at 13:57
add comment

Well, I couldn't figure it out, but my brother, who is REALLY GOOD at PHP did figure it out. He himself doesn't consider this the best of solution, but hey, it works. Maybe a quick-fix of which he isn't proud, but I am saved! ;-) He was even so kind as to comment his work. I hope someone else can use this as well!

<?php

$args=array(
    'author_email' => '',
    'ID' => '',
    'karma' => '',
    'number' => 5,
    'offset' => '',
    'orderby' => 'comment_date_gmt',
    'order' => 'DESC',
    'parent' => '',
    'post_id' => '',
    'post_author' => '',
    'post_name' => '',
    'post_parent' => '0',
    'post_status' => 'publish',
    'post_type' => 'page',
    'posts_per_page' => 1,
    'status' => 'approve',
    'type' => '',
    'user_id' => '',
    'search' => '',
    'count' => false,
    'group' => 'post_id'
);
$comments = get_comments($args);

// Due to wordpress not having a common functionality, we create a new list of comments manually
$filteredComments = array();
// Posts we already have a comment for
$filteredUsedPosts = array();
// Amount of comments
$filteredCommentLimit = 5;

// Loop through all comments
foreach($comments as $comment) {
    // Have we reached our amount of coments, then weŕe finished
    if (count($filteredComments) < $filteredCommentLimit){
        // Do we already have comment for this post?
        if (!in_array($comment->comment_post_ID, $filteredUsedPosts)){
            // We add the POST ID to the list of used POST IDs
            $filteredUsedPosts[] = $comment->comment_post_ID;
            // We add our comment to the display comments list
            $filteredComments[] = $comment;
        }
    }
}

foreach($filteredComments as $comment) :

    ?>
<div style="border: 0px solid #000000; height: 1%; overflow: hidden;">
    <div style="border: 0px solid #000000; float: left; width: 28%;">
        <a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo get_avatar( $comment, 48 ); ?></a>
    </div>
    <div style="border: 0px solid #000000; float: right; width: 72%;">
        <?php
        $aantalcomments = get_comments_number( $comment->comment_post_ID );

        ?>
        <a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo($comment->comment_author) ?></a> op:<br>


        <a href="<?php echo get_comment_link($comment->comment_ID); ?>">

            <?php
            echo($comment->post_name . " (" . $aantalcomments . ")<br>");
            ?>
        </a>
        <?php

        echo get_comment_date(' l, j F, Y ', $comment->comment_ID);

        ?>
    </div>
</div>
<hr>
<?php



endforeach;

?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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