Community Documentation

Alternating Teaser Classes

Last updated August 25, 2009. Created by Shane Birley on August 12, 2008.
Edited by bekasu. Log in to edit this page.

If you want to change the theme on alternating teasers, here is a quick way to accomplish this. First open your node.tpl.php (or whatever content type file you have created) and find:

<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">

Now, copy and paste the following code:

<?php global $node_count; print ($node_count++ % 2) ? 'odd' : 'even'; ?>

And paste it into the original code (into the class not the id). It should now look like this:

<div id="node-<?php print $node->nid; ?>" class="node-<?php global $node_count; print ($node_count++ % 2) ? 'odd' : 'even'; ?><?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">

When you save your file to your theme directory you will node "odd" and "even" being added on each teaser.

NOTE: This should also work with Drupal 4.x but the structure of the node.tpl.php is slightly different but should work fine.

Originally from http://drupal.org/node/12171.

About this page

Drupal version
Drupal 5.x, Drupal 6.x
Audience
Designers/themers, Site users

Theming Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.