Support » Fixing WordPress » wp_get_object_terms comma separated

  • Resolved greencode

    (@greencode)


    I’m using the following code to get a list of custom taxonomy terms that then get added to the post:

    <?php
    $product_terms = wp_get_object_terms($post->ID, 'skill');
    if(!empty($product_terms)){
      if(!is_wp_error( $product_terms )){
        echo '<ul>';
        foreach($product_terms as $term){
          echo '<li><a href="'.get_term_link($term->slug, 'skill').'">'.$term->name.'</a></li>';
        }
        echo '</ul>';
      }
    }
    ?>

    This is displayed as list of terms. What I would like is to have a comma separated list with the last item not having a comma.

    Is this possible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Does this work for you?

    <?php
    $product_terms = wp_get_object_terms($post->ID, 'skill');
    if(!empty($product_terms)){
      if(!is_wp_error( $product_terms )){
    
    	$numItems = count($product_terms);
    	$i = 0;
    
        foreach($product_terms as $term){
          echo '<a href="'.get_term_link($term->slug, 'skill').'">'.$term->name.'</a>';
    	  if (++$i != $numItems) {echo ', ';}
        }
    
      }
    }
    ?>

    Thread Starter greencode

    (@greencode)

    Thanks so much for this. That works perfectly 🙂

    Dibakar Jana

    (@themepurpose)

    Thanks @greendemiurge. This worked nicely.!!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_get_object_terms comma separated’ is closed to new replies.