列出标签 wordpress 时删除最后一个逗号 [英] Remove last comma when listing tags wordpress
本文介绍了列出标签 wordpress 时删除最后一个逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下脚本来列出没有链接的帖子的标签,但它在所有标签(包括最后一个)之后放置了一个逗号.有什么办法可以防止脚本向列表中的最后一个标签添加逗号吗?我试着研究它,但关于这个特定的 wp 字符串真的没有很多.
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 . ', ';
}
}
?>
推荐答案
使用 rtrim.它将修剪最后一个指定的字符.
Use rtrim. It will trim the last specified character.
$posttags = get_the_tags();
if ($posttags) {
$taglist = "";
foreach($posttags as $tag) {
$taglist .= $tag->name . ', ';
}
echo rtrim($taglist, ", ");
}
这篇关于列出标签 wordpress 时删除最后一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文