Post Pic

How to: Number your comments

Do your blog posts receive lots of comments? If yes, it can be really useful for both you and your readers to number it. Here’s how to do it easily and efficiently.

Open comments.php and find the following line:

<?php foreach ($comments as $comment) : ?>

Just above this line, initialize a counter variable:

<?php $i = 0; ?>

Just after this line, increment the counter:

<?php $i++; ?>

Now, you just have to echo the $i variable to get the number of the current comment. Paste this code anywhere on your comments loop:

<?php echo $i; ?>

Your comments are now numbered!

22 Responses

Feb 03 2009 09:36

In my opinion, the easiest way to number comments is to use ordered lists, but this is great if you want it to customize the way it looks!

Feb 03 2009 15:05

Jean, your code will output always 1.
Put the line $i = 0; above foreach.

Feb 03 2009 15:11

@Michael: I said “below” instead of “above” :/ Thanks for letting me know!

Feb 03 2009 20:22

This code comes handy if by default the theme isn’t using numbered list. Well, the theme I’m currently using by default has.

Feb 03 2009 21:14

I used to number comments that way but now WordPress 2.7 doesn’t use anymore. Whitout this loop numbering the comments is not that easy. Any thoughts on how to implement the numbering in WP 2.7?

Feb 03 2009 21:17

Sorry I meant that WP 2.7 doesn’t use the “foreach” anymore

Feb 04 2009 12:00

any news about 2.7???

Feb 05 2009 11:33

@Daniele @Danny: correct, i have write a solution for 2.7 and all other version of wordpress, easy to use: http://wpengineer.com/numbering-your-comments-pingbacks-trackbacks-or-all/

Feb 05 2009 16:10

Thank you Frank, great article!

Feb 05 2009 16:12

@Danny: Welcome; best regards.

Feb 05 2009 20:52

Haha i have many readers asking me about this. Well I guess its nice to be able to show your comment numbers, if you think they are impressive!

Ben

Feb 06 2009 13:19

it doesn’t work on wp2.7.

Feb 08 2009 14:41

It’s doesn’t work on WP 2.7 :(

Feb 08 2009 15:19

@Jauhari and !CnSoLoer

Frank, 4 comments above, has the solution!

Dec 04 2009 06:45

i like this… thanx!!!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required