I have written a custom walker that I am using with wp_nav_menu
but I need to iterate through 4 custom classes, and I’m not sure how to do this. Basically, each menu-item <li>
needs to be given a class of color1
, color2
, color3
or color4
, cycling back to color1
, lather, rinse, repeat, in that order.
Ordinarily I would do this with a $i
and a $i++
, but how do I do this in the Walker class?
EDIT: My Walker class code:
class Salamander_Advent_Walker extends Walker_page {
function start_el(&$output, $item, $depth, $args) {
if ( $depth ) {
$indent = str_repeat("t", $depth);
} else {
$indent = '';
}
$advent_thumbnail = get_post_meta($item->ID, 'advent-thumb', true);
$advent_slug = get_post_meta($item->ID, 'advent-slug', true);
$advent_oneliner = get_post_meta($item->ID, 'advent-oneliner', true);
//$description = get_post_meta($item->ID, 'advent-thumb', true);
$output .= $indent . '
<li class="active color4">
<a href="#day'. $advent_slug .'">
<span class="day">
<strong>'. $advent_slug .'</strong>
<span> </span>
</span>
<span class="content">
<small>'. $advent_slug .'</small>
<img src='. $advent_thumbnail .' width="126" height="91" alt="advent" />
<strong>'. $advent_oneliner .'</strong>
</span>
</a>';
}
}
Use a class variable to keep the ‘index’ of the colour you wish to display. Increment each time it’s used, and check if it goes over, in which case set it back to 1.
Create a separate function:
In your walker use it like this:
You can do that by setting the incrementing number to a global variable.