List x amount of categories in dropdown menu

Im trying to list a set amount of product categories (x) in a dropdown menu, but if the total amount of shop categories are greater than x then only list x minus 1 and display “view all categories” link in last place. What im trying to achieve is only x entries total in the menu so, whether it be categories or a ‘view all’ link.

Im a beginner and have exhausted my knowledge of if/else logic. Everything i have tried has messed up the results.

Here is the basic code that lists them in columns of 8 up to total of 32. Id like if there are 33 or more categories for the 32nd to be a link to all of them. If only 32 then just list all 32 with no link.

‘product_cat’,
‘orderby’ => ‘name’,
‘number’ => 32,
‘title_li’ => ”,
‘show_count’ => 0,
‘pad_counts’ => 0,
‘hierarchical’ => 1,
‘hide_empty’ => 0,
‘echo’ => 0,
‘exclude’ => ’73, 74, 16′,
‘depth’ => ‘1’,
‘style’ => ”,
);

$get_cats = wp_list_categories($args);
$cat_array = explode(“
“,$get_cats);
$results_total = count($cat_array);
//8 per column
$remainder = ($results_total-8);
$cats_per_list = ($results_total-$remainder);
$list_number = 1;
$result_number = 0;
?>

“>

= $cats_per_list) {
$result_number = 0;
$list_number++;
echo ”.$category.’ ‘;
}
else {
echo ”.$category.”;
}
}
echo ‘View All Categories‘;
?>

Read more here:: List x amount of categories in dropdown menu

Leave a Reply

Your email address will not be published. Required fields are marked *