Syndicate

Feed

Theming comments by the node's author

You want the comment style to be different for comments added by the author of a node — who's commenting on his own node. For example, you may want to highlight the node's author's comments, so that any visitor skimming through the comments will easily differentiate them from other comments.

Solution

  1. Edit comment.tpl.php to add a new class (let's say 'comment-by-author-of-post') that...

  2. you can style in your theme's style.css.

Your file comment.tpl.php may look like this before the edit (I am only providing a snippet of the template here):

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

If it does, then you will change the above snippet to that:

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

You could now add these two rules to your style.css file, for example:

/**
 * Comment styling rules
 */
.comment-by-author-of-post {
  border: 1px solid #ccc;
  background-color: #eee; 
}
/* reset to default values when previewing */
.preview .comment-by-author-of-post {
  border: none;
  background-color: transparent;
}

This will work in Drupal 5 as well. No, it won't. The $node object is not passed to the comment.tpl.php template. Read the comments below for the Drupal 5 recipe. This solution works only for PHPTemplate-powered themes. If your theme does not provide a template file for comments, copy the one from modules/comment/ to your theme folder, and edit it. And yes, the $node object is available in comment.tpl.php. It is the node the comment is attached to. Possible variations: special-style all comments authored by those who have the 'admin' role; special-style anonymously-submitted comments; etc.

[]

We used a Ternary Comparison Operator in our solution.

($a == 11) ? 'eleven' : 'not eleven'

The above code snippet returns a value. If $a equals 11, the return value is 'eleven'; otherwise, the return value is 'not eleven'. You can print the return value of a ternary comparison operator like so:

print ($a == 11) ? 'eleven' : 'not eleven';

Notice that I added both 'print' and a semicolon in the above snippet.

From php.net: “The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.” The ternary comparison operator is used a lot in Drupal templates. In our solution we printed ' comment-by-author-of-post' — in the class attribute of the comment div — if and only if the comment author was the same guy/girl as the author of the node the comment was attached to. And we printed nothing otherwise, ie: ''. (These '' are two SINGLE quotes.) We added this:

<?php print ($comment->uid == $node->uid) ? ' comment-by-author-of-post' : ''; ?>
Last edited by Caroline Schnapp about 3 years ago.

Comments

Drupal 5?

I am sorry, but this one seems not working with Drupal 5? The snippets from the template for Drupal 5 is supposed to be this one:

status == COMMENT_NOT_PUBLISHED) print ' comment-unpublished'; ?> ">

And I've changed it to:

status == COMMENT_NOT_PUBLISHED) print ' comment-unpublished'; print ($comment->new) ? ' comment-new' : ''; print ($comment->uid == $node->uid) ? ' comment-by-author-of-post' : ''; print ' '. $status ?> ">

But it's still not working. Any help?

Thanks for the attention. :)

In Drupal 5, no $node object in comment.tpl.php

What you do have is the nid of the node the comment is attached to.

So what you need to do is pass the node's author uid, along to comment.tpl.php.

Something like this needs to be done in the file template.php of your theme:

function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'comment':
      // The following will require no database query,
      // because the node is in the cache
      $node = node_load($vars['comment']->nid);
      $vars['node_uid'] = $node->uid;
      break;
  }
  return $vars;
}

Then you can use $node_uid in comment.tpl.php — that'll be the uid of the node author.

<div class="comment<?php if ($comment->status == COMMENT_NOT_PUBLISHED) print ' comment-unpublished';  print ($comment->new) ? ' comment-new' : ''; print ($comment->uid == $node_uid) ? ' comment-by-author-of-post' : ''; print ' '. $status; ?>">

Just one author?

Just one blogger on your web site? Do not bother with checking who is the author of the node. Do not bother with passing an additional variable to your comment template.

In the case where you have only one blogger... you could do as I do here (and yes, my web site still runs on Drupal 5...). Using a hard-coded value for the author of the node, eg: 1 (the Administrator of the site):

<div class="comment<?php print ($comment->uid == 1) ? ' comment-by-author-of-post' : ''; print ($comment->new) ? ' comment-new' : ''; print ($comment->status == COMMENT_NOT_PUBLISHED) ? ' comment-unpublished' : ''; print ' '. $zebra; ?>">

Thanks

Sorry for my noobness, but thanks! It works wonderfully on my site. Thanks, once again.

It was my mistake

It was my mistake.

I corrected my 'tutorial', and I am now saying:

this will work in Drupal 5 as well. No, it won't. The $node object is not passed to the comment.tpl.php template. Read the comments below for the Drupal 5 recipe.

Thanks to you!

Thanks for the confirmation

Thanks for the confirmation - had to come back to tell you it didn't work... but of course now it does!

woaw, thanks for the code

woaw, thanks for the code.

Great

it is useful informations and i want to thank for efforts and your explain

useful work

it is useful to share here with great informations and new ideas

Thanks for the good info. I

Thanks for the good info. I saw this happening on some websites, blood diamonds but didn't know how to implement it.

Ah. Thanks for the good

Ah. Thanks for the good info. I saw this happening on some websites, but didn't know how to implement it. Thanks Caroline!

Drupal 5

Thanks for the comments on Drupal 5. I was having trouble. Now I know why.

Good Info

Good Info, thanks

Nice write up

Nice write up

Thanks for the comments in

Thanks for the comments in Drupal 5. I had problems. Now I know why.

heartburn no more book

Which Drupal version does this work for?

Hi guys,
Just reading the comments, what version of Drupal does the above example work for? I read issues with Drupal 5 so does this mean its a Drupal 6 solution? Or is it Druapl 4?

I'm looking for the Drupal 6 solution.

Cheers,
Gavin.

PS: What WSYIWIG are you using with Drupal?

Hi Gavin

what version of Drupal does the above example work for?

Drupal 6.

What WSYIWIG are you using with Drupal?

None. I use BUEditor. It's not WSYIWIG per say, although it's an editor and has a really nice ajaxified preview button. I loooove it: http://drupal.org/project/bueditor.

Ah, finally. I was wondering

Ah, finally. I was wondering why I was having trouble.

Off to implement this code...

i want to thank for efforts

i want to thank for efforts and your explain

awesome

This is such a great idea, but I have not seen many sites using it. I wonder why. It would really help me, so I am glad to see you have created this theme. Thanks so much for the instructions.

this works

This works rally well, and I think it is crucial to the comments on a blog to be able to distinguish this. thanks

I would agree that this is

I would agree that this is an awesome fix to a problem I've been having for almost a month! Thank you for the insight.. Going to change it right now.

Thank you so much for the

Thank you so much for the information

Thanks

Thanks! Good info!

good tutorial

admin,
I think I will update my site following your tutorial.
Hope it is easy like you explained it because I'm a beginner.
Page bookmarked.

it works

thanx for your information
it works
nice article

Great information

As a noob and has no knowledge in php programming, this infomation can greatly teach me on how to do hard things easy.

Theme coding for authors/admins

Edited the comment.tpl.php and added the two rules to the css file and it worked like a charm. Thanks Caroline, for providing the code, I was spending a lot of time searching Google and couldn't find anything on how to change the admin highlighting style to distinguish them from regular users until I found this post.

- Brian

I looked all over google for

I looked all over google for it!! Thanks for the info..pfff! :)

nice

It took me a lot of time to found the info! I'm in assurance voiture moins chère and I needed for my websites about car insurance.

What about the CAPTCHA

How did you create this unique CAPTCHA? Are you using any commercial extension?

I tried to Edit

I tried to Edit comment.tpl.php to add a new class,i can not do so.Please tell me the way.
Cross Channel Mojo

Your file comment.tpl.php

Your file comment.tpl.php may look like this before the edit (I am only providing a snippet of the template here): Your file comment.tpl.php may look like this before the edit (I am only providing a snippet of the template here):

I am usually to blogging and

I am usually to blogging and i actually respect your content. The article has actually peaks my interest. I'm going to bookmark your website and maintain checking for brand spanking new information.

comments.tpl.php

Had some problems with the php file at the beginning, but i think i got it nwo right. Thank you.

-Antti

php

I am not strong in php, but this sure helps me out, thanks will be back

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <css> <html> <javascript> <mysql> <php> <span> <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <sup> <sub> <dd> <del> <blockquote> <img> <q> <p> <div>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <javascript>, <mysql>, <php>, <rails>, <ruby>.

More information about formatting options

CAPTCHA
I have to wonder if you're a human spammer or a machine, or less likely someone who cares to leave his or her thoughts behind.