How to add classes to every X number of node in a array How do I write a preprocess_node function to add a class to every second/third node it generated?

Scenario: add .last to every 2nd node, so that i can apply margin-right: 0; for the last column in the row.
For node listings for which the node.tpl.php (or a different node template) is being used you have variables like $zebra and $id at your disposal that you can use for this kind of tasks. You can see all the available variables with descriptions in the node modules node.tpl.php file or here.

For example if you want to add the necessary class to this line:

&lt;div id="node-&lt;?php print $node-&gt;nid; ?&gt;" class="&lt;?php print $classes; ?&gt; clearfix"&lt;?php print $attributes; ?&gt;&gt;


you could use code snippets like these:


&lt;?php $classes .= ' '.$zebra; ?&gt; - add 'odd' to every first and 'even' to every second node in the list
&lt;?php if (!($id % 2)) $classes .= ' last'; ?&gt; - add 'last' class to every second node.
&lt;?php if (!($id % 3)) $classes .= ' last'; ?&gt; - add 'last' class to every third node.