WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to have alternating classes on the list created by wp_list_pages.

Currently I have extended the Walker_page class updating the start_el function as follows:

class Sidebar_walker extends Walker_page {

var $alternate = 'background_1';

function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
    if ( $depth )
        $indent = str_repeat("\t", $depth);
        $indent = '';

    extract($args, EXTR_SKIP);
    $css_class = array('page_item', 'page-item-'.$page->ID);

    $alternate = ($alternate != 'background_1') ? 'background_1' : 'background_2';
    $css_class[] = $alternate;

    if ( !empty($current_page) ) {
        $_current_page = get_post( $current_page );
        if ( in_array( $page->ID, $_current_page->ancestors ) )
            $css_class[] = 'current_page_ancestor';
        if ( $page->ID == $current_page )
            $css_class[] = 'current_page_item';
        elseif ( $_current_page && $page->ID == $_current_page->post_parent )
            $css_class[] = 'current_page_parent';
    } elseif ( $page->ID == get_option('page_for_posts') ) {
        $css_class[] = 'current_page_parent';

    $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );

    $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';

    if ( !empty($show_date) ) {
        if ( 'modified' == $show_date )
            $time = $page->post_modified;
            $time = $page->post_date;

        $output .= " " . mysql2date($date_format, $time);

Only background_1 is printing. how do I get $alternate to actually update.

share|improve this question
up vote 0 down vote accepted

Consider this a complement to toscho's solution.

I believe that ...

$this->alternate = ($this->alternate != 'background_1') ? 'background_1' : 'background_2';
$css_class[] = $this->alternate;

... will do what you want.

The difference is that toscho's solution, using the static keyword, will make that variable static for any instantiation of this walker-- that is, all instances will share the same value. If you instantiate two walkers the second will pick up where the last left off. That may be what you want, or it may not be. Using $this will limit it to the one instance of the walker only.

share|improve this answer
Worked perfectly thank you – jppower175 Apr 30 '13 at 20:04

Each time the method start_el() is called it starts from scratch and doesn’t know anything about earlier calls. To make it remember earlier states use the static keyword:

function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
    static $alternate = 'background_1';

Now the last state of $alternate will be kept for all calls.

share|improve this answer
That definitely solved the problem of $alternate updating. Now I have a new problem. It is not consistently switching between background 1 and 2. Because I am only printing the children of the current page, is $alternate updating even when something doesn't echo? – jppower175 Apr 30 '13 at 19:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.