您只需添加一个参数:
/* user commnet count */
function get_comment_count( $user_ID ) {
global $wpdb;
$count = $wpdb->get_var(
$wpdb->prepare( "SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE user_id = %d ", $user_ID )
);
return $count;
}
<?php echo get_comment_count( <USER_ID> ); ?>
另外,我还在您的查询中添加了一些适当的转义,因此此代码不再易受攻击。。。
但你也可以使用
$count = get_comments( array(
\'user_id\' => <USER_ID>, // include only comments by this user
\'count\' => true // it will return only count of comments and not the comments
) );
echo $count;