Created
March 15, 2016 17:19
-
-
Save jserrao/d4390ec7d9e946601038 to your computer and use it in GitHub Desktop.
Create four equally sized unordered lists <ul> with indeterminate amount of list items <li>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* This is trickier than it sounds | |
* You have to think about getting the number of list items and then dynamically generating the lists | |
* In HTML, you have to know when to close the lists too but there aren't finite objects in this model | |
* The following example is using WordPress but you could do this on any PHP app | |
*/ | |
// 1- Setup WP_query variable to get all location posts | |
// posts_per_page=-1 shows all locations | |
// orderby=title, orders locations alphabetically by title | |
// order=ASC, orders locations a-z | |
$args = array( | |
'post_type' => 'locations', | |
'posts_per_page' => 50, | |
'orderby' => 'title', | |
'order' => 'ASC' | |
); | |
$the_query = new WP_Query( $args ); | |
// 2- Setup variables | |
$location_count = $the_query->post_count; // total number of locations with post_count | |
$location_key_array = range(0, $location_count - 1); // sets up array key number based # of locations | |
$location_list_items = array_fill_keys($location_key_array, ''); // assigns keys to locations array | |
$c = 0; // sets up counter for WP while loop | |
// 3- Assign each location item into the location_list_items array use WP_query while loop | |
if($the_query->have_posts()) { | |
while($the_query->have_posts()) { | |
$the_query->the_post(); | |
// 3b- Setup variables to get state name for location | |
$stateID = get_field('state', $queried_object_id); | |
$stateName = get_term($stateID, 'states'); | |
// 3c- Put each location's HTML data into location_value string | |
$location_value = '<li class="locations-list-item locations-list-item-' . $the_query->current_post . '"><a href="' . get_the_permalink() . '">' . get_the_title() . ', ' . $stateName->name . '</a></li>'; | |
// 3d- Assign location_value string to $location_list_items array position, $c | |
$location_list_items[$c] = $location_value; | |
// 3e- Increment counter for next while loop run | |
$c++; | |
} | |
} | |
// 4- Set length of the location lists by diving total locations by 4, always rounding up | |
// You always round up because you want each list to be as long as possible, with the fourth being uneven with the others if necessary | |
$location_items_per_list = ceil( $location_count / 4 ); | |
// 5 - Set up location list range markers | |
$location_loop_start_position = array( | |
0 => 0, | |
1 => $location_items_per_list, | |
2 => ($location_items_per_list * 2), | |
3 => ($location_items_per_list * 3), | |
4 => ($location_items_per_list * 4), | |
); | |
// 6- Loop through location items based on ranges from location_loop_start_position markers | |
// loop #1 runs 4 times, creates a list, runs 2nd for loop each time | |
for( $i = 0; $i < 4; $i++ ) { | |
echo '<ul class="locations-list">'; | |
// loop #2 runs over a range defined by how many items are in location_items_per_list | |
// loop condition uses min() to make sure $location_loop_start_position never exceeds total number of location items from $location_count | |
for ( $j = $location_loop_start_position[$i]; $j < min($location_loop_start_position[$i + 1], $location_count); $j++) { | |
echo $location_list_items[$j]; | |
} | |
echo '</ul>'; | |
} | |
// 7- Reset post info so Wordpress can function and do queries elsewhere | |
wp_reset_postdata(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment