0
votes

How can i show Wordpress categories with subcategories comma separated? I got the following, but it's not comma separated per subcategory.

I want so specify everything from category id "1" show all child categories comma separated.

$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

1 Answers

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