0

I have a view that has a filter to only pull results from 2 content types. I would like each views entry to have a class that shows the content type of the node. How can I do it?

1

You can add that into the views fields template for the row or you can add a node type field to your view, and make sure it's first or before the field you want to add the class to, and disable its display. Then, you can go to the field you want to add the type class to, select rewrite output, and use the contents of this first field as a replacement value in the class of the wrapper element.

Just rereading, the above just wraps a field in your row. To put that class in the row's html, you could edit up views-view-fields.tpl.php for your specific view and add a div wrapper yourself with that information in it (grab it all out of the $row variable provided). Keep the node type field in there but have it not displayed.

So, for example, if your view is named foo, you could create a views-view-fields--foo.tpl.php with this in it:

<div class="<?php print $row->node_type; ?>">
<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php print $field->content; ?>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
</div>

where, if I am cutting and pasting right today, the only thing I've added is the opening div and closing div. This then wraps your whole row in your own div with class of nodetype.

You can find all of these templates in the views style information area (make sure your views code matches mine). Just save them in or under your theme directory.

ADDITION:

If you want to have the class in <div class="views-row views-row-2 views-row-even...."> instead of adding another <div class="...."> inside it, you can copy views-view-unformatted.tpl.php to one appropriately named for your view and grab the information needed for the node-type out of the $view variable there. You could also use a views_preprocess() function to do it, although for stuff like this I like seeing it in the template, but that's just me. The function should be mythemeormodulename_preprocess_views_view_unformatted(&$vars) and you can add or modify any of the vars you want, once again, since $view is present, all the information about the view is as well. For ideas, check out the views code in sites/all/modules/views/theme/theme.inc, or where ever module code is stored on your machine.

  • Thank you, that worked! I'm wondering, if I wanted the content type to be in each views row itself rather than a div within it, what would I do then? I tried editing views-view-unformatted.tpl.php and changed <div class="<?php print $classes[$id]; ?>"> to <div class="<?php print $classes[$id]; print $row->node_type; ?>"> but that didn't work. – MichaelK Mar 1 '12 at 22:10
  • if you want to do that, just don't click the "don't display" in the node type field itself. You probably want to turn off the label, too. – Jimajamma Mar 1 '12 at 23:14
  • If I did that I would have to use CSS to hide the content of the field from showing up in the view. Is there any other way around that? – MichaelK Mar 2 '12 at 19:55
  • I misunderstood. I don't know off the top of my head how to put a class in the html of the actual view rows div itself; obviously you can do it statically from the views ui itself, so programatically there must be a way. – Jimajamma Mar 2 '12 at 21:20
  • wow, this is exactly what I was looking for, thanks. – Danny Englander Jun 1 '12 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.