0

如何在 The Loop 之外显示用户的总评论数?

我使用此代码在循环内显示评论计数:

    <?php
    global $wpdb;
    $user_id = $post->post_author;
    $where = 'WHERE comment_approved = 1 AND user_id = ' . $user_id ;
    $comment_count = $wpdb->get_var(
        "SELECT COUNT( * ) AS total
    FROM {$wpdb->comments}
    {$where}
");
    echo 'Comments: <strong>' . $comment_count . '</strong>';
    ?>

这在循环内工作正常。为了使该代码在循环之外工作,我改为$user_id = $post->post_author;$user_id = get_the_author_meta( 'ID' );它不起作用。

我最接近的是这段代码:

                            <?php
                            global $wpdb;
                            $where = 'WHERE comment_approved = 1 AND user_id <> 0';
                            $comment_counts = (array) $wpdb->get_results("
                                SELECT user_id, COUNT( * ) AS total
                                FROM {$wpdb->comments}
                                {$where}
                                GROUP BY user_id
                                ", object);
                            foreach ( $comment_counts as $count ) {
                                $user = get_userdata($count->user_id);
                                echo 'Comments: ' . $count->total . '
                                ';
                            }
                            ?>

但是,这与所有用户的评论计数相呼应,例如:“评论:28 评论:11 评论:55”等

我可以使用什么代码在循环外显示用户的评论数

4

5 回答 5

4

尝试使一些事情更加全球化并以不同的方式获取用户数据。

<?php

global $wpdb, $post, $current_user;
get_currentuserinfo();
$userId = $current_user->ID;

$where = 'WHERE comment_approved = 1 AND user_id = ' . $userId ;
$comment_count = $wpdb->get_var("SELECT COUNT( * ) AS total 
                                 FROM {$wpdb->comments}
                                 {$where}");
echo 'Comments: <strong>' . $comment_count . '</strong>';
?>

或在functions.php中

<?
function commentCount() {
    global $wpdb, $current_user;
    get_currentuserinfo();
    $userId = $current_user->ID;

    $count = $wpdb->get_var('
             SELECT COUNT(comment_ID) 
             FROM ' . $wpdb->comments. ' 
             WHERE user_id = "' . $userId . '"');
    echo $count . ' comments';
}
?>

调用它

  <?php commentCount(); ?>
于 2012-09-14T14:34:24.773 回答
1

我知道这个话题很老,但只想删除一个更合适的片段:

<?php
$args = array(
    'post_id' => 1, // use post_id, not post_ID
        'count' => true //return only the count
);
$comments = get_comments($args);
echo $comments

?>

其中“post_id”可以是“user_id”

于 2016-09-23T15:27:50.107 回答
0

试试这个

$post= get_post($id=5);
$comment_count = $post->comment_count;
于 2013-08-16T23:21:42.447 回答
0

如果您正在观看他们的个人资料,您应该使用WHERE comment_author_email = "' . get_comment_author_email() . '"来获取每个用户的评论......@SMacFadyen 的代码将只给出登录用户的评论计数。

此外,第二个功能不起作用。

于 2013-05-13T13:24:26.130 回答
-1

作为一个函数,传递 $user_id - 也使用 $wpdb->prepare() 方法来格式化查询:

function get_comment_count( $user_id = null ) {

    if ( is_null( $user_id ) ) { return 0; }

    global $wpdb;

    return $wpdb->get_var( $wpdb->prepare(
        "
            SELECT COUNT( * ) AS total 
            FROM {$wpdb->comments}
            WHERE comment_approved = 1 AND user_id = '%d'
        ",
        (int) $user_id
    ));

}
于 2020-10-14T16:19:18.083 回答