WordPress 2.7 Theme Enhancements II

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:

  1. Automatically close comments on posts older than X days
  2. Enable threaded (nested) comments
  3. 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

< ?php
if ( post_password_required() ) { ?>
	<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
	< ?php
	return;
}
?>

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.

< ?php if ( have_comments() ) : ?>
	<h3 id="comments">< ?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;< ?php the_title(); ?>&#8221;</h3>

	<ol class="commentlist">
	< ?php wp_list_comments(); ?>
	</ol>
	<div class="navigation">
		<div class="alignleft">< ?php previous_comments_link() ?></div>
		<div class="alignright">< ?php next_comments_link() ?></div>
	</div>
 < ?php else : // this is displayed if there are no comments so far ?>

	< ?php if ('open' == $post->comment_status) : ?>
		<!-- If comments are open, but there are no comments. -->

	 < ?php else : // comments are closed ?>
		<!-- If comments are closed. -->
		<p class="nocomments">Comments are closed.</p>

	< ?php endif; ?>
< ?php endif; ?>

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

  1. If there are comments on the post…
  2. Echo our a h3 heading with how many comments there are
  3. Then uses the new wp_list_comments() template tag to list out the comments within an ordered list
  4. We then get the previous and next comments links which are there for the pagination feature should you choose to enable it.
  5. Else, if there are no comments…
  6. Check if comments can be made
  7. 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

<li class="comment byuser comment-author-sarah bypostauthor odd alt thread-odd thread-alt depth-1" id="comment-999">

	<div id="div-comment-999">
		<div class="comment-author vcard">
			<img alt='' src='http://www.gravatar.com/avatar/img.jpg' class='avatar avatar-32' height='32' width='32' />
			<cite>Sarah</cite> Says:
		</div>

		<div class="comment-meta commentmetadata"><a href="http://wordpress-address.com/?p=99&amp;cpage=1#comment-999">November 30th, 2008 at 10:00 am</a></div>

		<p>Comment content goes here.</p>

		<div class="reply"></div>
	</div>
</li>

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.

Other posts in this series

  1. Your Theme and WordPress 2.7
  2. WordPress 2.7 Theme Enhancements III

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on November 30th, 2008 and filed under WordPress Coding & Design
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx

16 Responses to “WordPress 2.7 Theme Enhancements II”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Thanks for the heads up. I am sure, I need to do a lot more to adapt my theme to 2.7… I am still sitting on 2.5 as I thought, I would do a one-time switch to 2.7.

    These platform upgrades happen too frequently that people are forced to spend a lot of time on other things than blogging

    Cheers, Ajith

  2. Sarah says:

    Hi Ajith, the changes between 2.5 and 2.7 are minimal. They may sound a lot but I’ve done them on my own theme and it took me around 30 minutes, mainly because I wanted to split up my comments and pingbacks (which I’ll be explaining how to do next week).

    As I mentioned, you only need to add in the changes to the comment code if you want to make use of the new features. If you don’t then you don’t need to change your theme. It will continue to work regardless.

    To be fair, I’d rather WordPress continually be improved offering the option of a variety of new features. You don’t have to use them, but they’re there if you want them. Much better than a stagnant blogging platform.

  3. rustin says:

    Very informative posting Sarah!

  4. Thank you for the nice heads up. I found your site very interesting and full of cool stuff that I might use. Keep up the good work.

  5. Ajay says:

    Hi Sarah,

    Just a question. When did post_password_required get introduced? Was it in 2.7?

    If so, wouldn’t the theme need to be backward compatible as well?

  6. Sarah says:

    Ajay, yes post_password_required() is a new 2.7 tag/function. Everything I’ve written about has been introduced in 2.7.

    I’m not sure what you mean be backward compatible. If you upgrade to 2.7 then you can use these features. If you release themes and want to have them work for both 2.6 and 2.7 then you will need to check the version number or check if the function exists first eg.

    if (function_exists(post_password_required) :

  7. Ajay says:

    Thanks for clarifying.

    I was talking about this on a theme developers point of view.

  8. Cole says:

    Thanks for the article. Very helpful. I am curious about one thing – I have set up my depth and post-author-admin styles but there seems to be a conflict. If the admin is replying (a depth comment), I want it to override the depth styles. Unfortunately, when it does override, the depth styles do not work at all and when they do work, the admin style does not. Any ideas?

  9. Sarah says:

    Hi Cole, it sounds like your styles need fixing in the right order. If you post up about this in the discussion forums – http://www.bloggingtips.com/forums/ and post up your two styles then we can look at them and work out how to make them work together. However as a quick point, the post-author-admin style will need to come after the depth style. My solution would be to specify the depth style to both depth and the post-author-admin ie.

    depthstylename, .post-author-admin {
    // depth styles
    }
    .post-author-admin {
    // additional admin styles
    }

    this way the post-author-admin style will inherit the depth styles and then just change what it needs to.

    However if that doesn’t work or doesn’t make sense, post up in the forum and we can help you there :)

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Find Out More About Our Newsletter
 

Blogging Tips Sponsors

Blogging Tips Newsletter

 

Blogging Tips Sponsors

 

Latest from the Blogosphere