将逗号添加到wordpress生成的列表中

时间:2015-02-18 11:17:27

标签: php wordpress

我有一个小函数可以修改Wordpress get_the_term_list函数创建的链接,但无法解决如何在列表项之间添加逗号(或其他分隔符)的问题。我的尝试添加了逗号,但也在列表的末尾添加了一个逗号,这是我不想要的。如何删除最后一个逗号?

$terms = strip_tags( get_the_term_list( $post->ID, 'portfolio_cat', '', '/' ));
$terms = explode("/",$terms);
for($i=0; $i<count($terms); $i++){ 
echo "<a href='urlhere/$terms[$i]'>" . $terms[$i] . "</a>";
$total = count($terms);
if ($i != $total) echo', ';
}

1 个答案:

答案 0 :(得分:1)

你很接近......但问题是因为你在$i开始0,而count()1开始。因此,要解决此问题,只需在总数中添加-1

$terms = strip_tags( get_the_term_list( $post->ID, 'portfolio_cat', '', '/' ));
$terms = explode("/",$terms);

for($i=0; $i<count($terms); $i++){ 
    echo "<a href='urlhere/$terms[$i]'>" . $terms[$i] . "</a>";
    $total = count($terms) - 1;

    if ($i != $total) { echo', '; }
}