Sometimes you want to show the number of total comments of a commentator in WordPress blog to emphasize the contribution of this commentator to blog's discussion. This can be done via simple queries to database as I'll show you in this article.
First, to customize the comments' look, we need to use the custom function to display the comments. In comments.php
file of your theme, replace the traditional function call:
wp_list_comments();
with:
wp_list_comments(array('callback' => 'twentyten_comment'));
the name of the callback function is custom, in this example I just take from the default Twenty Ten theme.
And then, in functions.php
file, define the callback function. This is the definition of Twenty Ten theme:
function twentyten_comment( $comment, $args, $depth ) { $GLOBALS['comment'] = $comment; ?> <?php if ( '' == $comment->comment_type ) : ?> <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>"> <div id="comment-<?php comment_ID(); ?>"> <div class="comment-author vcard"> <?php echo get_avatar( $comment, 40 ); ?> <?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?> </div> <?php if ( $comment->comment_approved == '0' ) : ?> <em><?php _e( 'Your comment is awaiting moderation.', 'twentyten' ); ?></em> <br /> <?php endif; ?> <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"> <?php /* translators: 1: date, 2: time */ printf( __( '%1$s at %2$s', 'twentyten' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' ); ?> </div> <div class="comment-body"><?php comment_text(); ?></div> <div class="reply"> <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> </div> </div> <?php else : ?> <li class="post pingback"> <p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Edit)', 'twentyten'), ' ' ); ?></p> <?php endif; }
Now we can create our custom function for displaying total comments of a commentator. Add the following code to functions.php
file:
function get_total_comments_by_author($id) { global $wpdb; $author = $wpdb->get_var($wpdb->prepare("SELECT comment_author FROM $wpdb->comments WHERE comment_ID='$id' LIMIT 1")); return $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_author='$author'")); }
Find the place where you want to put total comments in the custom callback function. For example, I want to put the total comments after the string "author says:" in Twenty Ten theme, so I find this line:
<?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
Add the following line after it:
<?php echo '(', get_total_comments_by_author(get_comment_ID()), ' comments)'; ?>
That's all. You can see the action in the following screenshot:
Leave a Reply