How to Highlight Author Comments in Drupal

Since I switched to drupal 6.x, I have been been looking for way to change the color of an author's comments. Having a distinctive color makes it simple, to quickly scan a thread to see when an author has left a comment. I solved the problem, at first I using an if statement, to add a class reference, then further refined the code using a ternary operator.

1. First edit comment.tpl.php to add a new class reference (i.e 'author-comment').

This is a snippet of the original comment.tpl.php before the editing:

<div class="comment<?php print ($comment->new) ? ' comment-new' : ''; print ' '. $status; ?>">

I used this if statement to add the class reference to the div . It checks if the comment author is the same person as the node's author the comment is attached.

<div class="comment<?php print ($comment->new) ? ' comment-new' : ''; if ($comment->uid== $node->uid): print ' '. ' author-comment'; else: print ' '. $status; endif; ?>">

Next I optimized the code by replacing the if statement with a ternary operator.

<div class="comment<?php print ($comment->new) ? ' comment-new' : ''; print ($comment->uid == $node->uid) ? ' author-comment' : ''; print ' '. $status;?>">

2. Next you need add this rules to your style.css file.

In this example only the author's comments will be themed with a yellow background and grey outline.

/** Drupal comments **/
.author-comment
{
      background-color: #ff5;
      border: 1px solid #aaa;
}

This method works in drupal 5.x, and drupal 6.x for PHPTemplate-powered themes.

This is a better solution than what I was using before, and works in D6, too. Thanks.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
copy the characters (respecting upper/lower case) from the image.