Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在wordpress中用点/句点替换最后一个逗号?_Php_Wordpress_String_Comma_Period - Fatal编程技术网

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>'; } ?>