Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function getRandomNumbers()
- {
- // We start with an empty array and add numbers until we hit 12.
- $result = array();
- // We choose either 3 or 4 as basic number.
- $x = mt_rand(3, 4);
- // Now, as long as we don't hit 12, we iterate through a loop,
- // adding either the basic number, or 2 times the basic number:
- while (array_sum($result) < 12) {
- // Randomly decide
- if (mt_rand(0, 1) > 0) {
- $newElement = 2 * $x; // This is either 6 or 8
- // However, always make sure not to exceed 12:
- if (array_sum($result) + $newElement > 12) {
- $newElement = $x;
- }
- } else {
- $newElement = $x; // This is either 3 or 4
- }
- // Add the new number to the array:
- $result[] = $newElement;
- }
- // Return the resulting array
- return $result;
- }
- // BEGIN LOOP
- if(have_posts()) :
- // Get the initial array with random numbers
- $randomArray = getRandomNumbers();
- // Open the initial row.
- echo '<div>';
- // CHECK FOR POSTS
- while (have_posts()) : the_post();
- // CREATE ROWS
- if (count($randomArray) < 1) {
- // Close the row and start a new one:
- echo '</div><div>';
- // Get a fresh array with random numbers:
- $randomArray = getRandomNumbers();
- }
- $nextRandomNumber = array_pop($randomArray); // This takes the next number.
- echo '<div class="col-md-' . $nextRandomNumber . '">';
- the_content();
- echo '</div>';
- // END
- endwhile;
- // Close the final row.
- echo '</div>';
- // CLOSE LOOP
- endif;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement