我有下面的脚本来列出没有链接的帖子的标签,但它在所有标签后面加了一个逗号,包括最后一个标签。
<?php
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            echo $tag->name . ', '; 
        }
    }
?> 发布于 2013-01-04 03:37:56
使用rtrim。它将修剪最后一个指定的字符。
    $posttags = get_the_tags();
    if ($posttags) {
       $taglist = "";
       foreach($posttags as $tag) {
           $taglist .=  $tag->name . ', '; 
       }
      echo rtrim($taglist, ", ");
   }发布于 2013-01-04 03:35:21
if ($posttags) {
    echo implode(
        ', ', 
        array_map(
            function($tag) { return $tag->name; },
            $posttags
        )
    );
}发布于 2013-01-04 03:50:00
当我需要连接可变数量的元素时,我倾向于这样做。
$posttags = get_the_tags();
if ($posttags) {
    foreach($posttags as $tag) {
        $temp[] = $tag->name; 
    }
}
if (!empty($temp)) echo implode(', ',$temp);https://stackoverflow.com/questions/14145877
复制相似问题