I can easily add NID (Node ID) as a field in my view and then render it as a class for the Views Row using replacement patterns for the row class of an unformatted typical view. (See screen capture).

enter image description here

However, I'd like to render the NID as an ID for each row for use for some special theming I am doing. If I use devel themer, I see my template is views-view-unformatted.tpl.php so I can of course copy this template into my theme's template folder with a custom name specific to my view and then theme it.

The relevant line of code in the template that renders the row class is:

  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>

If I change it to:

<div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?> 
    id="<?php print $fields['nid']->content ?>">

...However, this does not work as I don't think fields are available at this level in the views row.

So essentially right now my rendered output is:

<div class="views-row views-row-1 views-row-odd views-row-first mynid-63">
<div class="views-row views-row-2 views-row-even mynid-64">

... when I'd like it to be

<div class="views-row views-row-1 views-row-odd views-row-first" id="mynid-63">
<div class="views-row views-row-3 views-row-even" id="mynid-64">

My question, is there a way to just render the associated NID where I want it as an id other then the method I am trying to use?

  • maybe I am missing something but why can't you use row-nid-[nid] (or whatever prefix you want) in your Row Class and then use .row-nid-64 (or whatever nid ##) instead of #row-nid-64 in your CSS? Eg, what kind of theming are you doing that requires an ID instead of a CLASS?
    – Jimajamma
    Commented Apr 18, 2013 at 17:52
  • @Jimajamma No that won't work, I need an ID as specified in my question above, I am doing a custom responsive grid gallery and each row needs a unique ID attribute for later use. The NID is perfect for this as it's always going to be unique and relevant to the data. Commented Apr 18, 2013 at 17:56
  • Also, regardless of the outcome, be careful with numeric CSS IDs or classes -- lots of browsers don't like them, eg #64 is illegal/ignored.
    – Jimajamma
    Commented Apr 18, 2013 at 17:56
  • @Jimajamma - of course that's just an example, I can easily preface the NID (once I get it) with a textual name... Commented Apr 18, 2013 at 17:57

3 Answers 3


In most Views templates, the entire $view object is available. So, assuming you have a nid field in the row, you should be able to access it with something like this:


so, give

id="my-nid-is-<?php print $view->result[$id]->nid; ?>">

a try in your template.

If this doesn't work exactly, a print_r($view->result); up at the top of the template will give you a gander at what you have to work with.

  • Perfect, just what I needed, works great thanks. My google-foo failed me on this one but now that I know what I am looking for, there's probably an API doc somewhere for this. Commented Apr 18, 2013 at 18:23
  • glad I could help out! Some Views-foo: there will (almost always) be a nid field in the results when there is a node in there so you might not even need to include the nid field directly unless you need it in your View for something other than theming.
    – Jimajamma
    Commented Apr 18, 2013 at 18:29
  • Is there a way to achieve this inside a module (without using a custom template) ? Commented Aug 21, 2013 at 1:52
  • Off the top of my head I don't think so since the html is generated inside the template, and the default ones provided don't include any id tags in them at all.
    – Jimajamma
    Commented Aug 21, 2013 at 2:31

The NID is added via the format settings as last in the class string; ' $classes_array[$id]

Get the last class in the class string (which is the Nid added via views) Then just add the $last as proper ID to the row after the classes

so change this to:

<div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>


<div<?php if ($classes_array[$id]) { 
    $class_string = explode(' ', $classes_array[$id]);
    $last = end($class_string); 
    print ' class="' . $classes_array[$id] .'"' . ' id="' . $last . '"';  
 } ?>>

You need to use the views-view-fields--[name].tpl.php for construct the main row div. So, put in your `views-view-fields:

<div id="<?php print $row->nid; ?>">
<?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; ?>

The main problem is that you have two <div> tags. So you can choose to delete the div into views-view-unformatted.tpl.php.

  • Well if I delete the div in views-view-unformatted.tpl.php, that's no good as then I loose all my nice row classes and I don't think I can pop that code into the fields template. That's "robbing Peter to pay Paul" as it were... Commented Apr 18, 2013 at 16:35
  • you're right ;) Commented Apr 19, 2013 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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