Hi,
I want to have menu items with alternating styles, like the $zebra, but I have no idea how to get it to work here. I'm using this to create the menu items:

function phptemplate_menu_item($mid, $children = '', $leaf = true) {
 return '<li class="'. ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed')) .'">'. menu_item_link($mid) . $children ."</li>\n";
}

But this returns a list like this:

<li class="leaf">item 1</li>
<li class="leaf">item 2</li>

How can I modify it so it produces something like this:

<li class="odd">item 1</li>
<li class="even">item 2</li>
<li class="odd">item 3</li>
<li class="even">item 4</li>

Thanks for any help.

Comments

nevets’s picture

The following version will add odd/even to each menu item (it retains the existing classes also)

function phptemplate_menu_item($mid, $children = '', $leaf = true) {
	static $count = 0;
	$zebra = ($count % 2) ? 'odd' : 'even';
	$count++;
	return '<li class="' . $zebra . ' ' . ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed')) .'">'. menu_item_link($mid) . $children ."</li>\n";
}
00qazs’s picture

Hi, thanks very much!

arbel’s picture

Hello,

How would I go about modifying this in order to add odd/even to a list generated by a view in a block?

Thanks.

Idan

00qazs’s picture

Hi,
You can go to the block.tpl.php file and insert $zebra in the <div> tag like this: <div <?php print $zebra ?>>

[Edit to add <code> and </code> tags: nevets]

nevets’s picture

Note Drupal 5 already include a $block_zebra variable you can use.