Im trying to list a maximum of ‘x’ product categories (a multiple of y) with a qty of ‘y’ in each column. ‘x’ decides how many columns are displayed.
What I am trying to do: - If there are less product categories than ‘x’ in the shop then display all. - If there are exactly ‘x’ then display ‘x’. - If there are more than ‘x’ display x-1 and display a “view all categories” link in the last place.
As an example: With 35 product categories. If max product categories=32 and qty per column=8, display 31 product categories in 4 columns and the "view all" link in 32nd place.
The total product categories are constantly changing so I am trying to make the code work dynamically for any amount, without the need to change the code, unless to change 'x' or 'y'.
Here is the basic code that I have been able to make work for me, however the link spills into the next column, rather than last place in the previous column. As a beginner I have exhausted my knowledge of if/else logic and everything else I have tried is messing up the output.
<?php
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'number' => 32, //maximum to list
'title_li' => '',
'show_count' => 0, // 1 for yes, 0 for no
'pad_counts' => 0, // 1 for yes, 0 for no
'hierarchical' => 1, // 1 for yes, 0 for no
'hide_empty' => 0, // 1 for yes, 0 for no
'echo' => 0, // 1 for yes, 0 for no
'exclude' => '73, 74, 16', //best sellers, new, and uncategorized
'depth' => '1', //top level categories, not sub
'style' => '', //default is list with bullets, '' is without
);
// Grab top level categories
$get_cats = wp_list_categories($args);
// Split into array items
$cat_array = explode("<br />",$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many tags to show per list-8)
$remainder = ($results_total-8);
$cats_per_list = ($results_total-$remainder);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<div class="cat_columns" 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 '<div>'.$category.'</div> </div> <div class="cat_columns" id="cat-col-'.$list_number.'">';
}
else {
echo '<div>'.$category.'</div>';
}
}
echo '<a href="//">View Categories</a>';
?>
Im trying to list a maximum of ‘x’ product categories (a multiple of y) with a qty of ‘y’ in each column. ‘x’ decides how many columns are displayed.
What I am trying to do: - If there are less product categories than ‘x’ in the shop then display all. - If there are exactly ‘x’ then display ‘x’. - If there are more than ‘x’ display x-1 and display a “view all categories” link in the last place.
As an example: With 35 product categories. If max product categories=32 and qty per column=8, display 31 product categories in 4 columns and the "view all" link in 32nd place.
The total product categories are constantly changing so I am trying to make the code work dynamically for any amount, without the need to change the code, unless to change 'x' or 'y'.
Here is the basic code that I have been able to make work for me, however the link spills into the next column, rather than last place in the previous column. As a beginner I have exhausted my knowledge of if/else logic and everything else I have tried is messing up the output.
<?php
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'number' => 32, //maximum to list
'title_li' => '',
'show_count' => 0, // 1 for yes, 0 for no
'pad_counts' => 0, // 1 for yes, 0 for no
'hierarchical' => 1, // 1 for yes, 0 for no
'hide_empty' => 0, // 1 for yes, 0 for no
'echo' => 0, // 1 for yes, 0 for no
'exclude' => '73, 74, 16', //best sellers, new, and uncategorized
'depth' => '1', //top level categories, not sub
'style' => '', //default is list with bullets, '' is without
);
// Grab top level categories
$get_cats = wp_list_categories($args);
// Split into array items
$cat_array = explode("<br />",$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many tags to show per list-8)
$remainder = ($results_total-8);
$cats_per_list = ($results_total-$remainder);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<div class="cat_columns" 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 '<div>'.$category.'</div> </div> <div class="cat_columns" id="cat-col-'.$list_number.'">';
}
else {
echo '<div>'.$category.'</div>';
}
}
echo '<a href="https://www.aaaa/all-categories//">View Categories</a>';
?>
Share
Improve this question
edited Sep 10, 2019 at 3:17
aye cee
asked Sep 8, 2019 at 15:10
aye ceeaye cee
15912 bronze badges
3
- Not sure why this question has been down voted. I have posted the same question recently, which I believe was misinterpreted to some extent by the member who answered. They told me basically bad luck, to accept the answer and post a new question. So here it is, further clarified. I am not sure how to have this not considered a duplicate? Here is the previous question: wordpress.stackexchange/questions/346801/… – aye cee Commented Sep 10, 2019 at 2:56
- So, in your example, 'x' is 32 and 'y' is 8, right? – Sally CJ Commented Sep 11, 2019 at 0:10
- Yes thats exactly right – aye cee Commented Sep 11, 2019 at 0:31
1 Answer
Reset to default 1So this is different from your code (and the one in the accepted answer to the other question), but it worked well for me:
You can remove the "\t" .
and . "\n"
which I added for pretty formatting purposes so that you could better inspect the generated HTML markup. And let me know if you have any question on any parts of the code. :)
// First, define these.
$max_cat_count = 32; // this is 'x'
$qty_per_column = 8; // this is 'y'
// Then the $args array.
$args = array(
'taxonomy' => 'product_cat',
'number' => $max_cat_count + 1, // keep the + 1
'title_li' => '',
'hide_empty' => 0,
'echo' => 0,
'style' => '',
// ... your other args here ...
);
// Get the categories list.
$get_cats = wp_list_categories( $args );
// Split the list into array items.
$cat_array = explode( "<br />", $get_cats );
$total = count( $cat_array );
$list_number = 1;
$_new_col = false;
foreach ( $cat_array as $i => $category ) {
if ( $i >= $max_cat_count ) {
break;
}
if ( $i % $qty_per_column === 0 ) {
// Close previous column, if any.
if ( $_new_col ) {
echo '</div><!-- .cat_columns -->' . "\n";
}
// Open new column.
$id = 'cat-col-' . $list_number;
echo '<div class="cat_columns" id="' . $id . '">' . "\n";
$_new_col = true;
$list_number++; // increment the columns count
}
if ( $total > $max_cat_count && $i === $max_cat_count - 1 ) {
// Make sure to change the # to the proper URL.
echo "\t" . '<div><a href="#">View All</a></div>' . "\n";
} else {
echo "\t" . '<div>' . trim( $category ) . '</div>' . "\n";
}
}
// Close last column, if any.
if ( $_new_col ) {
echo '</div><!-- .cat_columns -->' . "\n";
}