I'm trying to list all terms on a custom taxonomy but I wanted to at least group them into 3 columns including their children to have a visual balance. Here's what I've done so far. I'm stuck on creating the loop after it reached the maximum term. On what I have, It wrapped all the succeeding items with 'ul' instead of creating a second ul and list the next batch. After it reaches the x amount of term it should create another 'ul' element listing categories in it. There will be a total of 3 columns.
<?php
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=2&hide_empty=0,&taxonomy=industries' );
// Split into array items
$cat_array = explode('</li>',$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many categories to show per list (round up total divided by 3)
$cats_per_list = ceil($results_total / 3);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<?php echo $cats_per_list ; ?>
<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number >= $cats_per_list) {
$list_number++;
echo $category.'</li> </ul> <ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else {
echo $category.'</li>';
}
}
?>
</ul>
The code is very buggy. Just a couple of observations:
Next in the
if($result_number >= $cats_per_list) {
Block you are going to have to reset result_number to 0 since the count starts over again. Your current code would only meet that condition once since $cats_per_list is defined as the average of the total amount. After that it would continue counting up and ALWAYS be >= $cats_per_list
Next: it's quibble but you probably don't need to ceil the result since you are using >=, that operation pretty much does the same thing since 1.5 will meet the criteria of >= 1 as a for instance.
Try this and see if it is any better:
<?php
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=2&hide_empty=0,&taxonomy=industries' );
// Split into array items
$cat_array = explode('</li>',$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many categories to show per list (round up total divided by 3)
$cats_per_list = ceil($results_total / 3);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<?php echo $cats_per_list ; ?>
<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number >= $cats_per_list) {
$result_number = 0;
$list_number++;
echo $category.'</li> </ul> <ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else {
echo $category.'</li>';
}
}
?>
</ul>