Php 如何在wordpress中用点/句点替换最后一个逗号?
我试图用重复出现的逗号替换:Php 如何在wordpress中用点/句点替换最后一个逗号?,php,wordpress,string,comma,period,Php,Wordpress,String,Comma,Period,我试图用重复出现的逗号替换:: 标题1,标题2,标题3 将来 标题1,标题2,标题3 我似乎在网上找不到任何解决方案,我看过很多rtrim和内爆解决方案,但不知道如何使用,以下是完整的代码: <?php if ( $posts->have_posts() ) { while ( $posts->have_posts() ) : $posts->the_post(); global $post; ?> <a class="link" href="<?ph
:
标题1,标题2,标题3
将来
标题1,标题2,标题3
我似乎在网上找不到任何解决方案,我看过很多rtrim和内爆解决方案,但不知道如何使用,以下是完整的代码:
<?php if ( $posts->have_posts() ) { while ( $posts->have_posts() ) : $posts->the_post(); global $post; ?>
<a class="link" href="<?php the_permalink(); ?>"><?php the_title( $before='', $after=', ' ); ?></a>
<?php endwhile; } else { echo '<h4>' . __( 'Link not found', 'shortcodes-ultimate' ) . '</h4>'; } ?>
非常感谢您的帮助。这里:
rtrim($string, ","); // this will remove the comma on the right.
$string .="."; // This will add a dot.
尝试定义循环中的
、
或
。你可以通过找出哪篇文章是最后一篇文章来做到这一点
<?php if ( $posts->have_posts() ) { while ( $posts->have_posts() ) : $posts->the_post(); global $post; ?>
<?php $commaAndDot = ($posts->current_post + 1 != $posts->post_count) ? ', ' : '.'; ?>
<a class="link" href="<?php the_permalink(); ?>"><?php the_title( $before='', $after=$commaAndDot ); ?></a>
<?php endwhile; } else { echo '<h4>' . __( 'Link not found', 'shortcodes-ultimate' ) . '</h4>'; } ?>
虽然这可能会回答问题,但最好添加一些说明,说明此答案如何帮助解决问题。请阅读了解更多。
<?php if ( $posts->have_posts() ) { while ( $posts->have_posts() ) : $posts->the_post(); global $post; ?>
<?php $commaAndDot = ($posts->current_post + 1 != $posts->post_count) ? ', ' : '.'; ?>
<a class="link" href="<?php the_permalink(); ?>"><?php the_title( $before='', $after=$commaAndDot ); ?></a>
<?php endwhile; } else { echo '<h4>' . __( 'Link not found', 'shortcodes-ultimate' ) . '</h4>'; } ?>