Every Third with Different Class Names

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(); ?>

Source.

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.