将逗号添加到 wordpress 生成的列表
add commas to wordpress-generated list
我有一个小函数可以修改由 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', ';
}
您很接近...但问题是因为您从 $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', '; }
}
我有一个小函数可以修改由 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', ';
}
您很接近...但问题是因为您从 $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', '; }
}