Skip to content
Advertisement

Show user’s total comment count outside The Loop in WordPress

How do I display a user’s total comment count outside The Loop?

I use this code to display comment count inside the loop:

JavaScript

That works fine inside the loop. In an attempt to make that code work outside the loop, I changed $user_id = $post->post_author; to $user_id = get_the_author_meta( 'ID' ); but it did not work.

The closest that I have been is with this code:

JavaScript

However, this echos comment count for all users, like this: “Comments: 28 Comments: 11 Comments: 55” etc

What code can I use to show the user’s comment count outside the loop?

Advertisement

Answer

Try making a few things more global and getting the user data differently.

JavaScript

or in functions.php

JavaScript

To call it

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement