要在WordPress中显示当前登录用户的评论数量,您可以使用以下代码示例。此代码应该添加到您的主题的functions.php文件中,或者您可以使用一个自定义插件来执行它。
function get_user_comment_count() {
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        $comment_count = get_comments(array(
            'user_id' => $user_id,
            'count' => true,
        ));
        if ($comment_count === 1) {
            $comment_text = '1 Comment';
        } else {
            $comment_text = $comment_count . ' Comments';
        }
        return '<span class="usercommentcount">' . $comment_text . '</span>';
    }
    return '';
}
// 添加一个短代码以在内容中显示评论数量
function user_comment_count_shortcode() {
    return get_user_comment_count();
}
add_shortcode('user_comment_count', 'user_comment_count_shortcode');
上述代码创建了一个名为get_user_comment_count的函数,该函数会检查用户是否已登录,如果是,则获取当前用户的ID并计算他们的评论数量。然后,它根据评论数量返回相应的文本。
接着,它创建了一个名为user_comment_count_shortcode的短代码,以便您可以在文章或页面中使用[user_comment_count]来显示当前登录用户的评论数量。
将上述代码添加到您的WordPress主题的functions.php文件中,保存文件后,您就可以在文章或页面中使用[user_comment_count]短代码来显示当前登录用户的评论数量。