Skip to content

Instantly share code, notes, and snippets.

@phlbnks
Last active September 7, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phlbnks/4ff428f387bfed30ab3a0cad24c50f08 to your computer and use it in GitHub Desktop.
Save phlbnks/4ff428f387bfed30ab3a0cad24c50f08 to your computer and use it in GitHub Desktop.
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