How to add classes to comments? I've put the following code above print render($content['comments']); in comment-wrapper.tpl.php of our theme to add question or answer class to every comment:


    foreach($content['comments'] as &$c) {
      if(is_array($c) && array_key_exists('#entity_type', $c) && $c['#entity_type'] == 'comment') {
        $uid = $c['comment_body']['#object']->uid;
        $c['#attributes']['class'][] = $uid == 1 ? 'answer' : 'question';
      }
    }


The generated HTML however has no question or answer classes. Please help me to correct the error.
I think your ternary statement is off in that code.

I would also suggest an alternative approach.  You can use hook_preprocess_comment() in a template.php file or in a custom module.  With this you can hook into the comment before it's rendered to the page, run each comment through an logic test and add a class to the entire comment, or add an html chunk before or after the comment's html.  See more on hook_preprocess and render arrays.