Reputation: 1825
I've got this function:
<?php
$categories = []; //array to store all of your category slugs
$category_list_items = get_terms( 'product_cat' );
foreach($category_list_items as $category_list_item){
if(! empty($category_list_item->slug) ){
array_push($categories, $category_list_item->slug);
}
}
foreach ($categories as $category) {
$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => $category, 'orderby' => 'rand' );
?>
This is good, it returns the slug of a category for me in a loop. The problem is, now I can't get the category name anymore. I've tried adding another array: '$names = [];' and with that copied the if statement in which I replaced slug with name. But that didn't take. Out all of the solutions that i've tried, I only get returned 'Array' of just the whole list of names. Does anyone have a solution?
Thanks!
Upvotes: 0
Views: 2495
Reputation: 883
<?php
$categories = array();
$names = array();
$category_list_items = get_terms( 'product_cat' );
foreach($category_list_items as $category_list_item){
if(! empty($category_list_item->slug) ){
$categories[] = $category_list_item->slug;
$names[] = $category_list_item->name;
}
}
foreach ($categories as $key=>$category) {
echo "Category Name :".$names[$key];
echo "Category Slug :".$category;
$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => $category, 'orderby' => 'rand' );
?>
Upvotes: 0