[英]Displays the total number of comments of the logged in user ( $user_id = get_current_user_id(); )
I would like to see the number of comments entered by the user connected ( $user_id = get_current_user_id();
). 我想查看连接的用户输入的评论数( $user_id = get_current_user_id();
)。 I have tried this code but it does not work. 我已经尝试过此代码,但是它不起作用。
<?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 $comment_count;
?>
Do you have any advice? 您有什么建议吗? What am I doing wrong? 我究竟做错了什么? Thank you. 谢谢。
You can use the get_comments
function provided by Wordpress to query all comments based on query arguments. 您可以使用Wordpress提供的get_comments
函数根据查询参数查询所有注释。 Here's your code refactored to use this function: 这是重构为使用此功能的代码:
<?php
global $current_user;
$userId = $current_user->ID;
$comment_args = [
'user_id' => $userId,
// `count` is set to true to return the number of comments per UserID
'count' => TRUE
];
echo get_comments( $comment_args );
?>
See the docs here for full reference on the get_comments
function. 有关get_comments
函数的完整参考,请参见此处的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.