Incrementing a class in a custom walker

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.

class Salamander_Advent_Walker extends Walker_page {
    private $color_idx = 1;
    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 color'.$this->color_idx.'">
            <a href="#day'. $advent_slug .'">
                <span class="day">
                    <strong>'. $advent_slug .'</strong>
                    <span>&nbsp;</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>';              
        $this->color_idx++;
        if ($this->color_idx > 4) {
            $this->color_idx = 1
        }
    }

}

Leave a Comment