我有这个功能:
<?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。但这并没有。在我尝试过的所有解决方案中, 我只返回了仅包含整个名称列表的”数组”。有没有人有办法解决吗?
谢谢!
#1
<?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' );
?>
评论前必须登录!
注册