Last week I explained about two of the three enhancements available in WordPress 2.7. This week it’s all about the new comment options.
Before I continue however, there seemed to be some confusion last week from a comment or two. These changes are enhancements. Your existing themes will still work with WordPress 2.7, you will just not get the benefits or be able to use some of the new features until these changes are made. So don’t suddenly think your site is going to break or stop working when you upgrade to 2.7!
New Comment Options
In the admin for 2.7 there are 3 new comment options:
- Automatically close comments on posts older than X days
- Enable threaded (nested) comments
- Break comments into pages
The first new feature doesn’t require any theme changes, however if you want to use either of the second two features then we need to be updating the comments.php theme file.
New Comment Code and Comment Pagination
The WordPress team have simplified the comment code a lot now, so that it’s very similar to how the loop works for posts. They’ve also added in a lot of auto features such as various classes to help you target the post author, alternating comments etc. So no more need to hack code around to do this yourself (or no more need for the author highlight plugin if you had it).
If you have already got a copy of the latest beta (version 3 at the time of writing), then open up comments.php under the default theme directory. If you haven’t got this then you can download just this file – default comments.php File
Password Protection Check
The first piece of new code in the file is
[sourcecode language="php"]< ?php
if ( post_password_required() ) { ?>
< ?php
return;
}
?>[/sourcecode]
This check is already in place in the old default comments.php file. As you can probably tell, it checks if the post is password protected and if it is, it requests the password.This has simplified the original version and is a lot more easier to read. It also means it’s more future proof so it’s worthwhile changing if you password protect your posts at all.
Comment Output
This is the point at which comments are checked for, and if they are it lists them out.
[sourcecode language="php"]< ?php if ( have_comments() ) : ?>
< ?php comments_number('No Responses', 'One Response', '% Responses' );?> to “< ?php the_title(); ?>”
-
< ?php wp_list_comments(); ?>
< ?php else : // this is displayed if there are no comments so far ?>
< ?php if ('open' == $post->comment_status) : ?>
< ?php else : // comments are closed ?>
< ?php endif; ?>
< ?php endif; ?>[/sourcecode]
This is where a great improvement has been made, by making the code similar to the loop code. Going through this line by line we have
- If there are comments on the post…
- Echo our a h3 heading with how many comments there are
- Then uses the new wp_list_comments() template tag to list out the comments within an ordered list
- We then get the previous and next comments links which are there for the pagination feature should you choose to enable it.
- Else, if there are no comments…
- Check if comments can be made
- Else state comments are closed
For point 2 I’ll also point out that the first parameter of the function comments_number() is ‘No Responses’ which is a bit of a waste in this code, as this will never see the light of day because this heading is only displayed if there are comments. Personally I would move the header above the first if statement, however I’ll offer up my own sample code later on.
The output for each comment now looks like the following HTML code
[sourcecode language="html"]
Comment content goes here.
[/sourcecode]
As you can see, we have a number of new CSS classes available to us without the need for plugins or theme hacking. In the class list for the list item we have
- comment
- Standard comment class available on every comment. If the ‘comment’ is actually a pingback or trackback you’ll have the class ‘pingback’ or ‘trackback’ respectively
- byuser
- This class is added if the comment is made by someone who is a registered user of (and logged into) the site.
- comment-author-sarah
- This is added for commentators who are registered users of the site. Of course ‘sarah’ is changed to the user’s nickname
- bypostauthor
- Added if the commentator is the post author – aka ‘Author Highlight’
- odd
- This, or ‘even’ is added for odd and even posts
- alt
- This is added to each alternating comment
- thread-odd
- This is the same as the odd class (and there is a thread-even), however it’s only added to the top level comments – only of use when using threaded comments
- thread-alt
- The same as the alt class however, as above, is only added to top level comments, which is only of use/interest when using threaded comments
- depth-1
- The class depth-X denotes which depth the comment is at, so depth-1 is the top level, depth-2 are replies to top level comments etc.
To then use these classes to your benefit you can add some new styles in to your CSS stylesheet, after the main comment styles (else they will overwrite your custom settings). I highly recommend highlighting the author’s comments on the post, and possibly having some sort of distinction between alternating comments. For a multi author blog the site owner could then have his own comments highlighted even more distinctly, and perhaps other site authors could have a different distinction too. So for example take this scenario
- Comments have alternating background colours – use ‘alt’
- Registered users have a different background colour to others – use ‘byuser’
- The site owner has a different background to everyone else – use ‘comment-author-nickname’ where the nickname is the nickname of the site owner
- Post authors get their comment highlighted – use ‘bypostauthor’
If you add the relevant classes in that order then each item will override the previous items.
Next week will be the final part in this enhancement series, where I’ll explain how to use the new threaded comments and also explain how to control your comment output a little more than the default settings.







Sarah Says: