How To Add Numbers To Your Comments in 3 Easy Steps [WordPress Tip]
In the past few weeks I’ve had a few people email me and ask me how I get the numbering to show up on my comments. It’s really more of a PHP question then a WordPress question, but it’s quite simple. There may be plugins which do this for you, but anytime I can have a simple fix and avoid the overhead of a plugin, I’m happy. The following tutorials is for someone with no PHP experience and basic HTML/CSS knowledge. Here’s how to number your comments in three steps.
Example:

Step 1
Locate and open your theme’s comments.php file. Theme files are found in wp-content/themes/themename/. In your comments.php file you’ll be looking for the following code:
1 | <?php if ($comments) : ?> |
Directly after that line, type <?php $i = 1; ?>, so that it looks like this:
1 2 | <?php if ($comments) : ?> <?php $i = 1; ?> |
What were doing here is saying ‘If there are comments, set the variable “i” equal to “1″‘. This will be the number for your first comment. If you set this to something else, like 15, your comments would begin numbering at 15.
Step 2
Next, look for the following code:
1 | <?php foreach ($comments as $comment) : ?> |
Any code that found in between the above foreach code and a special endforeach code will be run for each individual comment. It is in this block of code that you’ll need to enter the following code:
1 | <?php echo $i; ?> |
What this does is tell the browser to display the value of the “i” variable, which for the first comment would be “1″. So where would you put this? You position this in the HTML of your comments so that it is displayed in your desired location. Treat the above PHP code as if it were a single number. You can surround it with any HTML and CSS that you want.
For example:
1 2 3 4 | <?php foreach ($comments as $comment) : ?> <div class="comments"> <strong> <?php comment_author_link() ?> </strong> said: </cite><span class="commentnumber"><?php echo $i; ?></span> ... |
Step 3
The last step is to increment the variable so that each comment has a higher number then the previous one. To do this, locate the following code:
1 | <?php endforeach; ?> |
Directly before that line of code, enter <?php $i++; ?>, so that it looks like this:
1 2 | <?php $i++; ?> <?php endforeach; ?> |
$i++ increments the value of the “i” variable. Since this is done for each (thus the “foreach”) comment, each successive comment will have a higher number then the last one.
Done
And that’s it. In three steps you’ve just added numbers to your comments. It’s quite simple. Numbered comments are useful because they let you easily reference previous comments. Now you can have them on your blog!
Useful Articles
6 Responses
to “How To Add Numbers To Your Comments in 3 Easy Steps [WordPress Tip]”
1 Trackback(s)
- Oct 25, 2008: Getting Technical – Numbering comments | Mom Of 3 Girls

You make it sound so simple, but with my luck I will end up frying the whole system which is why I am going to make a backup before I attempt this.
Wish you had written this a while ago before I set out and searched numerous forums on how to do this. Then I just had to settle for a theme which supported it natively. :-)
I wish I found your website sooner. You’ve got great wp tips even for a noob like me.
Oh I could just hug you right now! :)
I’ve been looking for this in many hours now and finally I end up on your site and see how simple it is. Hehe.
Thanks a bunch!
I tried other sites for this and they didn’t work. This one did, thanks a lot! :)