Add Threaded Comments to Your Theme in WordPress 2.7

One of the improvements added in WordPress 2.7 was official support for threaded comments. Previously, this functionality was only available through plugins. But now that threaded comments are available, you need to make sure that your theme is compatible.

To do this, you need to add a couple of things to your theme. First open up header.php in your theme, and add this line in between the head tags, preferably right before the wp_head function.

<?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

This adds the javascript required for threaded comments to any page or post where you have comments enabled, but not on the index or archive pages.

Next open up comments.php in your theme and make sure this code is in there.

<ol class="commentlist">
<?php wp_list_comments(); ?>

This code should replace your comments loop. To see an example of this, see comments.php in the default theme in 2.7. Once this is done, you need to activate threaded comments. Go to “Discussion” under “Settings” in your WordPress admin, and check the box that says “enable threaded comments.”

Another important step is the CSS. You need to style your comments properly for the threaded comments to be as functional as possible. For an example, again check out the CSS from the default theme in 2.7. You also need to be aware of how many levels your comments will be nested in your CSS. WordPress allows you to set this in your backend. The default is 5. With those steps, you should be good to go.

The wp_list_comments is an important function for other reasons. It also gives you the ability to sort comments, add gravatars, add CSS hooks for even and odd comments and author comments. So even if you don’t want threaded comments, it is an important function to have in your theme, especially if you are developing themes for public release.

Update: It turns out following the instructions above didn’t work for me. You still need the js file in your header, but there are more changes to comments.php than I thought. The easiest thing to do is to just copy the comments.php file from the default theme in 2.7 to your theme and modify that file to your liking. This finally got threaded comments working for me.