Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiple modulus calculations for looping background colours
<?php
// $posts is some content to loop over and output. Perhaps 28 items.
// We want raindow coloured boxes so loop over 7 possible colours.
$posts_count = 0;
foreach ( $posts as $post ) :
$colour = '';
if ( $posts_count % 7 == 0 ) {
$colour = 'red';
} elseif ( $posts_count % 7 == 1 ) {
$colour = 'orange';
} elseif ( $posts_count % 7 == 2 ) {
$colour = 'yellow';
} elseif ( $posts_count % 7 == 3 ) {
$colour = 'green';
} elseif ( $posts_count % 7 == 4 ) {
$colour = 'blue';
} elseif ( $posts_count % 7 == 5 ) {
$colour = 'indigo';
} elseif ( $posts_count % 7 == 6 ) {
$colour = 'violet';
}
echo '<div class="' . $colour . '">' . $post . '</div>';
$posts_count++;
endforeach;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.