Comment Numbering in WordPress

Today, I want to share various options for my codes, thanks to which you can number comments in WordPress.

The topic of comment numbering in WordPress has been raised multiple times on blogs and forums. However, I have not come across one of the options anywhere, just as I have not seen all the collected options in one article. Also, some of the solutions, in my opinion, are not the best (with extra data queries, such as: get_option('page_comments')). All of this formed the basis of writing this post.

Before using the hacks below, it is important to pay attention to where to insert them. You can use hacks if the comment output in your template is not styled by default, i.e. a custom comment output function is used (see wp_list_comments()) or if the template was developed for a version below 2.7. In 2.7, a special function for comment output wp_list_comments() was introduced.

Open the theme file comments.php, find the function wp_list_comments() there and look at its arguments. If the callback argument is specified, then it is quite simple to number the comments: the code needs to be inserted at the beginning of the function specified in the callback argument (in the example below, it is mytheme_comment). Look for the function (mytheme_comment) in the theme file functions.php.

wp_list_comments( 'type=all&callback=mytheme_comment' );

If you didn't find wp_list_comments, then the code should be inserted after the line:

foreach ( $comments as $comment ) :

Numbering Threaded Comments in WordPress

Hacks for numbering threaded comments:

Chronological Numbering of Added Comments

  • Comment 1
    • Comment 2
    • Comment 4
  • Comment 3
<?php
// Numbering by the post publication order for threaded comments
global $com_id_num;
if( ! isset( $com_id_num ) ){ // to collect $com_id_num only once
	foreach( $GLOBALS['wp_query']->comments as $com ){
		if( $args['type'] == 'comment' && $com->comment_type != '' ){
			continue;
		}
		$com_id_num[ $com->comment_ID ] = ++$n;
	}
}
$cnum = $com_id_num[ $comment->comment_ID ];
// the variable $cnum contains the comment number

Use the variable $cnum where you need to display the number.

Threaded Numbering (as on this blog)

  • Comment 1
    • Comment 1.1
    • Comment 1.2
  • Comment 2
<?php
# Comment counter: $cnum - top level, $cnum_inner - nested
global $cnum, $incnum, $comment_depth;
// defining the initial number if pagination is enabled
$per_page = isset( $args['per_page'] ) ? $args['per_page'] : $GLOBALS['wp_query']->query_vars['comments_per_page'];
if( $per_page && ! isset( $cnum ) ){
	$com_page = (int) $GLOBALS['wp_query']->query_vars['cpage'];
	if( $com_page > 1 ){
		$cnum = ( $com_page - 1 ) * (int) $per_page;
	}
}
// counter
if( $comment_depth > 1 ){
	$cnum_inner = '.' . $incnum++;
}
else{
	$cnum++;
	$incnum = 1;
}

Use the construct

<?php echo $cnum ?><sub><?php echo $cnum_inner ?></sub>

where you need to display the number. In this code, $cnum contains the number of top-level comments, and $cnum_inner contains the ordinal number of nested comments.

Numbering Non-Threaded Comments in WordPress

Numbering when Comments are Paginated

<?php
# For regular numbering if comments are not threaded

# and are paginated (if not paginated, the code also fits)
global $cnum;
// defining the initial number if pagination is enabled
$per_page = $args['per_page'] ? $args['per_page'] : $GLOBALS['wp_query']->query_vars['comments_per_page'];
if( $per_page && ! isset( $cnum ) ){
	$com_page = (int) $GLOBALS['wp_query']->query_vars['cpage'];
	if( $com_page > 1 ){
		$cnum = ( $com_page - 1 ) * (int) $per_page;
	}
}
// counting
$cnum = isset( $cnum ) ? $cnum + 1 : 1;
// the variable $cnum contains the comment number

Use the variable $cnum where you need to display the number.

If Comments are Non-Threaded and Not Paginated

The most basic numbering. This type of numbering is described everywhere.

<?php
# For regular numbering if comments are non-threaded and not paginated
global $cnum;
$cnum = isset($cnum) ? $cnum+1 : 1; ?>

Use the variable $cnum where you need to display the number.

Plugins

Also, for threaded comment numbering, there is a plugin: Greg's Threaded Comment Numbering. The only feature of this plugin that is not considered in the hacks above is the principle of numbering threaded comments, where numbers are assigned to all levels of nested comments (see the picture):

Greg's Threaded Comment Numbering in action