In a view listing posts, I want a a visual difference on posts by the two different authors. With author in Relationships, getting a css class on a field works only with fields but then I loose "submitted by" which only shows using the whole content of the post. Using rewrite doesn't output anything using replacement patterns. I couldn't find where submitted by is generated, not in basis template anyway.

Any ideas?

Accepted answer

if ($variables['type'] == 'post') {
  // Your code here


Are you open to adding a preprocess function to your theme'stemplate.php file?

If so, you could add some logic to check the author, and add a css class to the $variables['classes'] array, which will be added by the template to the rendered content. 

Basis already implements this preprocess function in template.php as basis_preprocess_node(), so you could add something like this. For other themes, you may need to implement this hook.


function basis_preprocess_node(&$variables) {
  // Leave the existing code here!
  if ($variables['elements']['#node']->uid == THE_UID_OF_THE_SPECIAL_USER){
    $variables['classes'][] = 'my-amazing-new-css-class';

I'm assuming that the View uses Basis to display the rendered content. If this is on Seven, or another front-end theme, you may need to implement the hook in those theme's template.php file or in a custom module.

Thank you! Yes it is a sub theme of Basis, + I knew I needed some programing help (my expertise is in css but I struggle with php).

This would be for posts, and two different uid for either author, so my new class would be one or the other uid.

I see the preprocess function, can you code it more specifically? Am I supposed to translate the all caps into something?

Much appreciated!

not working, can someone get me the rest of the way there plz.

My suggestion will only work if your view is displaying an "Unformatted list" of "Content" (not fields). Basically what it does is to add a class to the display of the node.

Can you be more specific by "not working"? 

Also can you explain what you did? 

Give us something to work with here...

Thanks @argiepiano,

I do have a view displaying an "Unformatted list" of "Content". I get no extra class anywhere. To be clear, I'd be needing different classes on either of the 2 author's posts, displaying in the view together.

I've removed the Relationship to the author, that results in still no extra class.

This is my template.php of my sub theme of Basis named "pressspark".

function pressspark_preprocess_node(&$variables) {
  if ($variables['status'] == NODE_NOT_PUBLISHED) {
    $name = node_type_get_name($variables['type']);
    $variables['title_suffix']['unpublished_indicator'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="unpublished-indicator">' . t('This @type is unpublished.', array('@type' => $name)) . '</div>',
  if ($variables['elements']['#node']->uid == THE_UID_OF_THE_SPECIAL_USER){
    $variables['classes'][] = 'my-amazing-new-css-class';

Hi echo. You are almost there. When I wrote THE_UID_OF_THE_SPECIAL_USER I meant for you to actually hard-code the user id of the users you the class for.

So, if the user id of one of the users is 12 and the other 23, then something like:

function pressspark_preprocess_node(&$variables) {
  if ($variables['elements']['#node']->uid == 12){
    $variables['classes'][] = 'user-id-12-class';
  elseif ($variables['elements']['#node']->uid == 23) {
    $variables['classes'][] = 'user-id-23-class';


Or you can get even fancier, and ALWAYS add a special class to the nodes based on the user IDs, for example:

function pressspark_preprocess_node(&$variables) {
  $variables['classes'][] = 'user-id-' . $variables['elements']['#node']->uid . '-class';

Obviously, you can change the name of the css class as you wish

Yay, got it! Thank you so much for helping me through this.

I humbly ask help with the syntax to make this only - if node type "post".

if ($variables['type'] == 'post') {
  // Your code here

Echo, I wanted to mention a tool you may find helpful in the future. This will allow you to explore the content of variables, for example the array $variables in the preprocess function. Had you know about this, you could have dumped the content of that array, which would have allowed you to discover that there is a 'type' key that contains the type of the node:

  1. Install and enable devel
  2. Inside the preprocess function, insert a line like dpm($variables);
  3. dpm will output the content of variables when you visit a page that runs that preprocess function

BTW there are plenty other elements in that array, for example 'uid' which you could use to explore the author of the node (as in $variables['uid'], which is shorter than what I suggested in my previous answer).

I hope you make good use of dpm :) 


Thanks very much for this, I never got to using dpm in my time in Drupal, but it interested me, and now I will.

With author in Relationships, getting a css class on a field works only with fields but then I loose "submitted by" which only shows using the whole content of the post.

Using field display, and without having a relationship to the author defined, you should be able to:

  • Add a UID field and exclude from display
  • Add a "Content: Rendered node" field and configure it to display the entire entity, in teaser mode (for example). This should keep your "submitted by" settings.
  • Add a class to each row/result in the view under "Format > Settings" and then look for "Row class" to add something like uid-[uid] there, which will render as "uid-3" for user 3.


Good to know :-) I chose the template solution as it even writes the class on the single display of one post, and I'd assume this was for the view only. Thanks for adding this here.