Voulez-vous afficher le nombre de commentaires de chaque utilisateur à côté de son nom d’utilisateur ? L’affichage du nombre de commentaires de chaque utilisateur peut l’encourager à poster plus de commentaires. Voyons comment afficher le nombre de commentaires pour chaque utilisateur.
Instructions:
1. Ajoutez ce code au fichier functions.php de votre thème ou dans un plugin spécifique à votre site:
<? function commentCount() { global $wpdb; $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . get_comment_author_email() . '"'); echo $count . ' comments'; } ?>
2. Ensuite, ajoutez cet extrait à votre fichier comments.php à l’endroit où vous souhaitez afficher le nombre de commentaires
<?php commentCount(); ?>
Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez vous référer à notre guide sur comment copier/coller correctement des extraits de code dans WordPress, afin de ne pas casser accidentellement votre site.
Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : Les meilleurs constructeurs de pages WordPress (comparés).
How do we add this into Users panel? Can we have a new column next to Username, Name, Email, Posts, that would list a comment count, and which you can sort by?
get_comment_author_email() this function not working current user email not find
I allow anonymous posting and end up with user_id = 0 in the comment table, how do I omit the return of this value?
if (user_id > 0) echo commentCount();
I’m guessing something along these lines would work or would it be better to edit the sql queries?
Yes, you may be able to use something like that if it’s consistent. The important thing is to test it out to make sure everything is working.
thanks for this.
Add the first snippet to the functions.php of your wordpress theme, then add the second snippet in the location you want to display the comment count. The place you may want to add it would be beside the users name “comment_author()” you should see that line within your comments.php.
Hi!
I want to display name of the user who is currently logged in and after logs out his name should be with the comment.
i’ve used session but when i logout the session destroys an dthe name and the comment disappears because of unsetting the session.
Thank You!
Hi Kevin, thank you for this tutorial 🙂
I was wondering…do you know how to add the comment for each user as a sortable column in the Admin panel (the user list)?
Thank you in advance for your help,
Roselle
How would you use Transients API with this to cache it? #wordpress
Sent you a link to a great post about this via twitter.
is this only for disqus system?
No this is not just for disqus, should work with any wordpress themes.
I dont see comment author on either comments.php
I am trying to get it in the skeleton theme of wptouch
I have never used that theme but if you add the commentCount() within the comment loop comments should be displayed within a unordered list. It will work, I would have to see the theme file to be sure. Use http://pastebin.com/ to post your code if you wish,
I dont see comment_author
where do we add it?
Add the first snippet to the functions.php of your wordpress theme, then add the second snippet in the location you want to display the comment count. The place you may want to add it would be beside the users name “comment_author()” you should see that line within your comments.php.