WordPress.org

Ready to get started?Download WordPress

Forums

Post Tags in WordPress (3 posts)

  1. ironman95
    Member
    Posted 1 year ago #

    Hey guys,

    i have a simple problem.
    I want use this code to print out the tags of a post:

    <?php
    $posttags = get_the_tags();
    if ($posttags) {
      foreach($posttags as $tag) {
        echo $tag->name . ', ';
      }
    }
    ?>

    The result is for example this "tag1, tag2, tag3,"

    What can I do to make the last comma away.

    Thank You.

  2. ricoh
    Member
    Posted 1 year ago #

    $posttags = get_the_tags();
    
    if ( ! empty($posttags) ) {
        $ltag = end($posttags);
    
        foreach($posttags as $tag) {
    
            if ($tag->name == $ltag->name ) {
              echo $tag->name;
            } else {
              echo $tag->name . ', ';
            }
        }
    }
  3. ironman95
    Member
    Posted 1 year ago #

    Thank you very much ricoh.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.