I have the following script to list a post’s tags without links, but it puts a comma after all of the tags including the last one. Is there any way to prevent the script from adding a comma to the last tag in the list? I tried researching it, but there really isn’t a whole lot out there about this particular wp string.
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ', ';
}
}
?>
Use rtrim. It will trim the last specified character.
I tend to do this when I need to concat a variable number of elements.
Change the placement of that Comma and put a small condition
You need the wordpress function
the_tags
. It will echo a tags so you won’t need the whole loop.