WordPress的:如何显示类别和子分类逗号分隔
问题描述:
我如何显示WordPress分类与逗号分隔子类别? 我得到了以下内容,但它不是逗号分隔每个子类别。WordPress的:如何显示类别和子分类逗号分隔
我想指定从类别ID“1”的所有内容显示所有子类别逗号分隔。
$categories = get_categories(); // Current Category will retrive
foreach ($categories as $category) {
$cat = $category->name; // Parent Category name
$category = get_category_by_slug($cat);
$args = array(
'type' => 'post',
'child_of' => $category->term_id,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => FALSE,
'hierarchical' => 1,
'taxonomy' => 'category',
);
$child_categories = get_categories($args);
$category_list = array();
$category_list[] = $category->term_id;
if (!empty ($child_categories)){ // If child Category available
foreach ($child_categories as $child_category){ // Print Child Category
$category_list[] = $child_category->term_id;
echo ",". $child_category->cat_name."<br/>";
}
}
}
答
$childCategoriesArray=Array(); // just for sake of safety
if (!empty ($child_categories)){ // If child Category available
foreach ($child_categories as $child_category){ // Print Child Category
$category_list[] = $child_category->term_id;
$childCategoriesArray[]=$child_category->cat_name;
}
}
}
echo implode(',',$childCategoriesArray); // that's it - comma separated :)