December 05, 2018
I have used counting classes so many times, but never understood them to this extend until I found this example.
<?php $args = array( 'posts_per_page' => -1, 'post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'asc', ); $pages = get_posts($args); $n = 0; foreach($pages as $post) : setup_postdata($post); $n++; if(get_field('show_on_home')) { ?> <article class="container <?php if(($n % 3) == 0) { echo 'grey'; } // every third, add class to first in sequence else if (($n % 3) == 1) { echo 'red'; } // every third, add class to second in sequence else if(($n % 3) == 2) { echo 'dark-red'; } // every third, add class to third in sequence ?> "> <?php } // end if get_field endforeach; wp_reset_postdata(); ?>