Title: wp_list_pages() Even/Odd function
Last modified: August 19, 2016

---

# wp_list_pages() Even/Odd function

 *  [anabelle](https://wordpress.org/support/users/anabelle/)
 * (@anabelle)
 * [18 years ago](https://wordpress.org/support/topic/wp_list_pages-evenodd-function/)
 * Hi, Im looking for a way to add a different classes to even an od items under
   a page listing, so I can make an alternating background effect in my sidebar.
 * How can I do it?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [18 years ago](https://wordpress.org/support/topic/wp_list_pages-evenodd-function/#post-763461)
 * anabelle,
    A possible solution is pasted below. Please note that this will only
   show “top level” pages. No child pages will display in the results. Just copy
   and paste the following code into your themes functions.php file:
 *     ```
       add_filter( 'wp_list_pages', 'mf_striped_page_filter' );
       function mf_striped_page_filter( $content ){
       	if( !empty($content) )
       		$lines = explode( "\n", $content );
   
       	$findme = '<li class="';
       	$replace = $findme . 'stripe ';
       	$count = 1;
       	foreach( (array)$lines as $k => $l ){
       		if( 0 === strpos( $l, $findme ) ){
       			if( $count % 2 == 0 )
       				$l = substr_replace( $l, $replace, 0, strlen( $findme ) );
   
       			$o .= "\n" . $l;
       			$count++;
       		}
       	}
       	return $o;
       }
       ```
   
 * This function will add a class of “stripe” to all `<li>` elements generated by
   wp_list_pages that are “top level”.
 *  [brockangelo](https://wordpress.org/support/users/brockangelo/)
 * (@brockangelo)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/wp_list_pages-evenodd-function/#post-763726)
 * this is some nice work mfields – I’ve never really played around with the hooks
   and filters much, but I started to after I saw this.
 * Could this be modified so that unordered lists alternate background color? It
   looks like you’d replace ‘the wp_list_pages’ with ‘the_content’, but I’m not 
   sure where your getting the $content and $lines from…

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wp_list_pages() Even/Odd function’ is closed to new replies.

## Tags

 * [alternating](https://wordpress.org/support/topic-tag/alternating/)
 * [even](https://wordpress.org/support/topic-tag/even/)
 * [odd](https://wordpress.org/support/topic-tag/odd/)
 * [wp_list_pages](https://wordpress.org/support/topic-tag/wp_list_pages/)

 * 2 replies
 * 3 participants
 * Last reply from: [brockangelo](https://wordpress.org/support/users/brockangelo/)
 * Last activity: [17 years, 7 months ago](https://wordpress.org/support/topic/wp_list_pages-evenodd-function/#post-763726)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
