Your problem is that you are defining the $typeName
variable as an empty array at the stat of each iteration of the loop, effectively erasing it, then filling that empty array with a single term name, which you implode
. You don’t see any commas because you are implode
ing a one term array. Move the definition to before the Loop and the implode
to after it.
$terms = get_the_terms($post->ID,'category');
$typeName = array();
foreach ( $terms as $term ) {
$typeName[] = $term->name;
} ?>
<small><?php echo implode(', ', $typeName); ?></small><?php
That said, there are more WordPress-ie ways to do this. WordPress provides a function called wp_list_pluck()
that will shorten your labor:
$terms = get_the_terms($post->ID,'category');
$typeName = wp_list_pluck($terms,'name'); ?>
<small><?php echo implode(', ',$typeName) ;?></small><?php
get_the_term_list()
may also work for you, though you get hyperlinks and not bare term names:
$terms = get_the_term_list( $post->ID, 'category', $before="", $sep = ', ', $after="" ); ?>
<small><?php echo $terms; ?></small><?php