我想显示由逗号分隔的链接项目列表,最后一个项目除外。我似乎语法不正确
<?php $prerequisite = get_field('prerequisites'); ?>
<?php if( $prerequisite ): ?>
<li> Prerequisites:
<?php foreach( $prerequisite as $prerequisites ): ?>
<a href="<?php echo get_permalink( $prerequisites->ID ); ?>"><?php echo get_the_title($prerequisites->ID ); ?></a>
<?php endforeach; ?>
</li>
<?php endif; ?>
答案 0 :(得分:1)
尝试此代码
使用 trim 函数逗号分隔值管理,您可以在此处找到 trim 函数详细信息:https://www.w3schools.com/php/func_string_trim.asp
<?php
$prerequisite = get_field('prerequisites');
if( $prerequisite ):
?>
<li> Prerequisites:
<?php
$nvtest = "";
foreach( $prerequisite as $prerequisites ):
$nvtest.='<a href="'.get_permalink( $prerequisites->ID ).'">'.get_the_title($prerequisites->ID ).'</a> ,';
endforeach;
echo trim($nvtest," ,");
?>
</li>
<?php endif; ?>