WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Remove last comma. Simple quest. Please help

[Resolved] Remove last comma. Simple quest. Please help

  • Hi all. I am a newbie to wordpress. I am trying to remove the last comma from this syntax:

    <li data-tags=”<?php if($terms) { foreach ($terms as $term) { echo $term->name . ‘,’; } } ?>”>

    This output something like:

    <li data-tags=”Logo Design,Print Design,”>

    And I don’t want the last comma.

    A little help would be great.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Try it with something like this (not tested):

    <?php if($terms) {
    $the_terms = '';
    foreach ($terms as $term) {
    $the_terms .= $term->name .',';
    }
    echo '<li data-tags="'.trim($the_terms, ',') .'">';
    }
    ?>

    Untested:

    <?php if($terms) {
    	$str = "";
    	foreach ($terms as $term) {
    		$str = $str .$term->name . ',';
    	}
    	?>
    	<li data-tags="<?php echo substr($str,0,($len-1)); ?>">
    <?php } ?>

    Edit:
    How was that two replies!

    HTH

    David

    Thank you very much for your prompt replies, both syntaxes work like a charm.

    Moderator keesiemeijer

    @keesiemeijer

    Glad you got it resolved.

    @digital Raindrops
    great minds think alike, those two code examples are nearly identical! (even the untested bit)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Remove last comma. Simple quest. Please help’ is closed to new replies.