Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add separator to get_the_category WordPress

  • SOLVED

Hello!
I'm using the code below to list out a portfolio item's categories (with no link). Works great, but it outputs the categories one right after another with no separator. I'd like to add a comma to separate them, if possible.


<?php
$categories = get_the_category();
foreach($categories as $category) {
if ($category->category_parent == 0) continue;
echo str_replace('-', '', $category->name).' ';
}
?>


Thanks!
Mike

Answers (1)

2011-08-04

Julian Lannigan answers:

Try this:
<?php
$categories = get_the_category();
$temp = array();
foreach($categories as $category) {
if ($category->category_parent == 0) continue;
$temp[] = str_replace('-', '', $category->name);
}
echo implode(", ", $temp);
?>


Mike McAlister comments:

Perfect, Julian. Thanks!

Mike