0

I am looking for a solution to separate the tag names by comma. The tag name i am using is without a link. Here goes the code:

<?php $my_tags = get_the_tags();?>
<?php echo $my_tags[0]->name?><?php $count=1;
foreach ($my_tags as $my_tag) {
if ($count != 1) {?>
<?php echo $my_tag->name?>
<?php }
$count++;
} ?>

The above code i got it from this page --> How to separate the tags?. But, the outcome is without a comma(,) if there are more than 1 tag.

Any solutions? Thanks!

0

1 Answer 1

1

This is actually more php related. Best will be to add your term names in an array and then use implode to convert it to a comma separated string

$my_tags = get_the_tags();
if ( $my_tags ) {
    foreach ( $my_tags as $tag ) {
        $tag_names[] = $tag->name;
    }
    echo implode( ', ', $tag_names );
}
1
  • The foreach can be omitted by using wp_list_pluck()
    – Anastis
    Mar 22, 2018 at 13:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.