Wordpress Tip: How to Add Comment Numbers
July 22nd, 2007
I’ve been running this blog for over a year now, and one of my favorite things to do is learn something new, then share it! This is something that won’t apply to everyone, as some already know how to do this, and others may already be using a theme with this implemented.
What I learned to do was add numbers to each of the comments on my site. It’s not overally difficult, so I figured I would share it with everyone. Here are the steps I took to number my comments. I don’t know if this is the best way, but it works!
- First thing you will want to do is backup your comments.php file.
- Go in to edit your theme and locate the comments.php file.
- Locate the code that starts the comment loop. It will look something like this:
<?php if ( $comments ) : ?> - Place this code immediately above the code in Step 3:
<?php $i = 0; ?> - Now locate the code that looks like this:
<?php foreach ($comments as $comment) : ?> - Placed this code immediately below the code in Step 5:
<?php $i++; ?> - Now use this code where you want to display your comment numbers:
<span class="count">
<?php echo $i; ?>
</span> - Now go to your stylesheet (style.css) and place this code anywhere on the stylesheet:
.count {
float:right;
padding: 12px 25px 0px 0px;
font-size:24px;
color:#3B5998;
}
You can adjust the stylesheet to fit your placement needs. Above is the code I used to place mine as you see it.











Cool tip Kyle .
Hmmm…I wonder whether that would be more efficient than actually doing a count query to the database. It would look something like this:
$i = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1' AND comment_type = '' AND comment_post_ID = $post->ID");
Setting the comment_type equal to ” means that it is a comment and not a trackback/pingback. I’m not guaranteeing that it will work out of the box, but you should be able to use the query and replace the code from steps 3 through 6.
Also, looking at the preview it looks like my single quotes might be replaced with “fancy” ones, so you would probably have to delete them and type your own in if you’re using that statement.
Madhur – No problem. I learn as I go, and try to share what I learn!
Ryan – Wow. I definitely concede to you when it comes to coding, as all of my coding knowledge comes from experimentation and trial/error. I will have to give that a try when I get a chance later tonight. Useful nuggets like this are always appreciated Ryan!
My next project is trying to figure out why I’ve been having problems getting trackbacks/pingbacks to show. I’ve gone through Wordpress’ help section and tried everything they recommend and can’t get them to show correctly, despite everything seeming to be correct. If I ever figure this out, I’ll probably be posting how I did it as they don’t seem to display correctly in some themes (mine is self-made obviously, but is based on a theme someone else made where they didn’t work correctly).
Nice writeup Kyle.. and thanks Ryan for the code nugget! I have been meaning to do this and getting the MBL avatars in the comments. Maybe if I get some time this week.
Also, I have mixed feelings about Threaded Comments.. what do you guys think about it?
Karthik – I am somewhat of a fan of the threaded comments, but I wasn’t able to get it to look the way I wanted and I prefer AJAX comments so the page doesn’t completely reload.
Really it just depends on what type of blog. If there is a need to respond directly to specific comments, then it is useful (in cases where some deep discussions getting started). Most blogs don’t have that need, so it is better to format the columns to look the way you want on your site in my opinion.
Kyle – Where’s an example of how the trackbacks don’t display properly. I know more than I care to admit about WordPress so I can probably help you out.
Karthik – I would think twice about using nested comments. We surveyed many of our dedicated commenters (over 20 users) on our site about two-months ago, and the almost unanimous response was “what are nested comments?” They didn’t understand how it worked, and after I explained it they said it would just make it more difficult to find the new comments. That’s something you should think about.
Ryan – Thanks for the offer! I sent you an e-mail with the coding information if you have time to look at it.
Great points about threaded comments too, I remember that.
That is exactly what kept me on the wall too, Ryan. I found it cool when you following a long discussion but as you said it is easy only to follow your own discussion but not all new comments. I guess I’ll save it for the day when my posts receive 100s of comments.
Thanks for the input.
No problem! It’s something I thought everyone would love as well, but after looking for some insight there wasn’t as much excitement around threaded comments as I expected. I think it is more confusing than anything for most people, much like how new Gmail users often get confused by their emails getting grouped as “conversations”.
Ryan – Perfect analogy. Oh, and thanks for the e-mail help. I will be giving that stuff a try this weekend hopefully when I have the time.
Very good tip, Kyle. Will come into use if I ever make a WordPress theme
it’s a great tip! i’ve been looking the way to show comment number in front of every comment since i launched my blog..
thank you for the tip..
Nice one there. I may need this when I decide to put number in my comment section. current theme doesn’t support it !
Benedict – Thank you for the Stumble!
You know what. I have implemented it and I’m thankful to you.!
Benedict – Glad this helped!!
thanks man
Thanks for sharing. I can see this is a wordpress blog but you have not adopted here. Was not satisfactory in the end?
Thanks, this was a big help!