0

I have a plugin to add a 'like' to a comment, each 'like' is stored in a table called likes_comments, I'm trying to sort the output of wp_list_comments according to how many likes each comment has, I'd like the comment with the most likes to appear at the top.

Here's what I'm using to call wp_list_comments:

<?php global $wp_query;
    $comment_arr = $wp_query->comments; usort($comment_arr, 'comment_comparator');
    wp_list_comments('callback=my_callback', $comment_arr);
?>

And here's my function:

function comment_comparator($a, $b)
{
$compared = 0;
if($a->likes_comments != $b->likes_comments)
{
    $compared = $a->likes_comments < $b->likes_comments ? 1:-1;
}
return $compared;
}
if($a->likes_comments == 0)
{
$compared = $compared2;
}

I'm completely stuck with this, any help at all would be greatly appreciated.

4

1 回答 1

1

为了使它工作,将代码放在functions.php AND in 和 in 中(您需要在调用incomments.php之前找到确切的放置位置):wp_list_comments()comments.php

函数.php:

function comment_comparator($a, $b)
    {
        $compared = 0;
        if($a->comment_karma != $b->comment_karma)
        {
            $compared = $a->comment_karma < $b->comment_karma ? 1:-1;
        }
        return $compared;
        }

评论.php:

global $wp_query;
    $comment_arr = $wp_query->comments;
    usort($comment_arr, 'comment_comparator');
    wp_list_comments('callback=gtcn_basic_callback', $comment_arr);

重要提示:此过程在使用插件评论评级时有效(大拇指向上 - 大拇指向下)。在此处阅读其他信息。

于 2014-01-07T13:00:37.137 回答