博客吧在制作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]; ?> |
以上两种方法输入的结果都是当前已登录用户的总评论数量。
原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/248414.html