一个分享WordPress、Zblog、Emlog、Typecho等主流博客的教程网站!
当前位置:网站首页 > WordPress教程 > 正文

wordpress调用显示当前登录用户评论数量

作者:xlnxin发布时间:2021-04-15分类:WordPress教程浏览:428


导读:在制作wordpress前台会员中心时,需要在会员中心主页展示当前登录用户的评论数量,因此检索了wordpress相关函数,发现通过get_comments()函数即可轻...

在制作wordpress前台会员中心时,需要在会员中心主页展示当前登录用户的评论数量,因此检索了wordpress相关函数,发现通过get_comments()函数即可轻松获取登录用户的评论数,下面是获取的完整代码。

方法一:

通过get_comments()函数获取

1
2
3
4
<?php 
global $user_ID;
echo get_comments('count=true&user_id='.$user_ID);
?>
方法二:

通过$wpdb查询数据库获取

1
2
3
4
5
6
7
<?php 
global $wpdb,$user_ID;
$count_user_comments = $wpdb->get_var( 
	$wpdb->prepare("Select count(comment_ID) from $wpdb->comments where user_id = %d", $user_ID) 
	);
echo $count_user_comments[0];
?>

以上两种方法输入的结果都是当前已登录用户的总评论数量。

标签:wordpress数据库