我试图在下拉菜单中列出一定数量的产品类别(x),但如果产品类别总数大于x,则只列出x减去1,并在最后显示“查看所有类别”链接。我试图实现的是每列只有8个条目(包括查看所有链接),总共有4列。
我是个初学者,已经用尽了if/else逻辑的知识。我尝试过的一切都把结果搞砸了。
下面是基本代码,它以8列的形式列出它们,总计32列。我想如果有33个或更多的类别,第32个类别将成为所有类别的链接。如果只有32个,那么只列出所有32个,没有链接。
<?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.com/all-categories//">View Categories</a>\';
?>