Reputation: 3
I am struggling with this piece of code. I do not want a dot after the last tag, what line do I write so that there is no dot after the last tag?
<?php
$id = get_sub_field('case_link');
$posttags = get_the_tags($id);
$tags = '';
if ($posttags) {
foreach ($posttags as $tag) {
$tags .= $tag->name . ' · ';
}
}
$tags = trim(substr($tags, 0, -2));
?>
Upvotes: 0
Views: 36
Reputation: 1757
add a counter and check if you are on the last iteration, if so add the name without the dot.
$id = get_sub_field('case_link');
$posttags = get_the_tags($id);
$tags = '';
$i = 0;
$len = count($posttags);
foreach ($posttags as $tag) {
if($i != $len-1) {
$tags .= $tag->name . ' · ';
}
else {
$tags .= $tag->name;
}
$i++;
}
Upvotes: 1
Reputation: 15923
Simply use rTrim() function
rtrim($yourString, 'ChartoRemove');
For your case
rtrim($tags, '·');
Upvotes: 1