Adding number to comments (to each of them)

Published: On Friday, April 16, 2010

How to add a number before each comment can happen to be bigger problem than it seems. Hopefully my instructions will at least put you on the right path.

Basic set-up

  1. Open functions.php, find the function describing a comment's template.
  2. inside the function, just at the beginning, add: static $comment_number; $comment_number ++; to accomplish incrementing of the number.
  3. somewhere inside the function body add echo $comment_number; to print the number. (Eventually including the php opening marks: <?php echo $comment_number; ?>)
comments numbering screenshot

Screenshot

Which function in functions.php is the comment-templating?

Look into comments.php in your template folder, search for wp_list_comments – it's parameter is the name of the function in functions.php you are looking for.

What if I use paged comments?

The code to insert is little more complicated. Follow the instructions from previous part ↑, just the incrementing code (point 2.) would be like this:

static $comment_number;
if (!isset($comment_number))
        $comment_number = $args['per_page'] * ($args['page'] - 1) + 1;
else
        $comment_number++;

What if I use threaded comments?

Then numbering your comments doesn't make much sense, does it?

What if the comment template is not defined in functions.php?

Then you should add it.

  1. Open comments.php and search for wp_list_comments. Inside its brackets add 'callback=my_start_el'.
  2. Into functions.php add this function (actually modification of the default theme's comment template)
    function my_start_el($comment, $args, $depth) {
            static $comment_number;
            $comment_number++;
    
            $depth++;
            $GLOBALS['comment_depth'] = $depth;
    
            $GLOBALS['comment'] = $comment;
            extract($args, EXTR_SKIP);
    
            if ( 'div' == $args['style'] ) {
                    $tag = 'div';
                    $add_below = 'comment';
            } else {
                    $tag = 'li';
                    $add_below = 'div-comment';
            }
    ?>
            <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
    
            <?php echo $comment_number; ?>
    
            <?php if ( 'div' != $args['style'] ) : ?>
            <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
            <?php endif; ?>
            <div class="comment-author vcard">
            <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
            <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
            </div>
    <?php if ($comment->comment_approved == '0') : ?>
            <em><?php _e('Your comment is awaiting moderation.') ?></em>
            <br />
    <?php endif; ?>
    
            <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'&nbsp;&nbsp;','') ?></div>
    
            <?php comment_text() ?>
    
            <div class="reply">
            <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
            </div>
            <?php if ( 'div' != $args['style'] ) : ?>
            </div>
            <?php endif; ?>
    <?php
    }

Comments

  1. 1.  Raymond wrote: On July 23., 2010 comment number 1

    Your basic setup method worked for post with all comments in one page.

    However when it comes to paged comments, there is a bug. For some weird reason the main page of the post starts with the number with 25. If the total of comments is 275, it will end up as 299 for the last comment.

    You can see the problem at my blog.
    http://www.ra­ymond.cc/blog/ar­chives/2010/07/­19/access-pandora-radio-outside-of-usa-ip-hider

    Please suggest a fix if you can. Thanks.

  2. 2.  Kahi [author] wrote: On July 23., 2010 comment number 2

    ↪ Raymond Much thanks for the report, I'll try to fix it in a few following days.

  3. 3.  Raymond wrote: On July 29., 2010 comment number 3

    Found any fix yet?

  4. 4.  Kahi [author] wrote: On July 30., 2010 comment number 4

    ↪ Raymond This is correct code for you:

    static $comment_number;
    if (!isset($comment_number))
            $comment_number = $args['per_page'] * ($args['page'] - 1) + 1;
    else
            $comment_number++;
  5. 5.  Raymond wrote: On August 18., 2010 comment number 5

    Thanks Kahi. Works perfectly.

I quit working with WordPress, comments are closed. My plugins will not be updated any more – at least not by me. Feel free to modify my source codes though… Also I am not able to provide support, sorry. –Kahi