我有这个功能:
<?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' );
?>
这很好,它在循环中为我返回一个类别的slug。问题是,现在我不能再获得类别名称了。我尝试添加另一个数组:'$ names = [];'并用它复制了if语句,其中我用名字替换了slug。但那并没有采取。在我尝试过的所有解决方案中,我只返回了整个名单列表中的“数组”。有没有人有解决方案?
谢谢!
答案 0 :(得分:0)
<?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' );
?>