Copyright © 2008 John Kolbert. Visit the original article at http://simply-basic.com/posts/1941334.
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:
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:
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!

|