I have next code for WordPress loop of tags for single post:
<?php if ($tags) : foreach($tags as $tag): ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>,
<?php endforeach; endif; ?>
I have comma added to last anchor. There is also white space after comma.
How can I remove the comma from last anchor while I am doing this with foreach() PHP loop?
Thanks for ideas and help!
Check if your loop is working on the last one:
What has a higher cost, calling a function or setting a variable? Here’s another way to do it perhaps, which sets a variable and removes the offending chars at the end – no extra math or if checks needed.
You can do it the other way around (remove it from the first one). If your array is numeric you can try something like this:
You can also try it using counter.
Output: value, value, value