Wordpress:如何以逗号分隔显示类别和子类别

时间:2017-10-11 14:06:54

标签: php 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/>";
    }
}
}

1 个答案:

答案 0 :(得分:1)

$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 :)