-1

I am trying to get taxonomies terms names separated with a comma. This is the code, but it separates taxonomies and terms with : If there are many terms they get separated with : instead of , If I delete the : symbol there are nothing in between taxonomy terms names.

<ul class='apt-product-terms'>
    <?php
        $_taxonomies = array(
          'taxname1'   =>   __( 'Taxname1', 'textstringdomain' ),
          'taxname2'   =>   __( 'Taxname2', 'textstringdomain' ),
          'taxname3'   =>   __( 'Taxname3', 'textstringdomain' ),
          'taxname4'   =>   __( 'Taxname4', 'textstringdomain' )
        );
      
      
        foreach ($_taxonomies as $taxonomy_slug => $taxonomy_name) {
            $terms = get_the_terms( $post->ID, $taxonomy_slug);
            if (is_array($terms) && count($terms) > 0) { ?>
               <li class='apt-tax-item'>
                  <span class='apt-term-name'><?php echo $taxonomy_name  ?></span>
                  <ul class='apt-tax-term-list'>
                     <?php foreach ( $terms as $term ) { ?>
                     <li class='apt-term-item'>: 
                        <a class='apt-term-link' href="<?php echo get_term_link($term); ?>"> <?php echo $term->name ?></a>           
                     </li>
                     <?php } ?>
                  </ul>
               </li>
            <?php            
            }
        }  
    ?>
</ul>

Where is the problem. I think it might be related to echo $term->name

Vel
  • 9,027
  • 6
  • 34
  • 66
twelvell
  • 257
  • 1
  • 8
  • 19
  • There is only one `:` character in that code. Try changing that. – 404 Not Found Mar 28 '22 at 17:19
  • Even if I delete that : the taxonomies are not comma-separated – twelvell Mar 28 '22 at 17:20
  • 1
    To be clear, do you want commas between _taxonomies_ or their _terms_? I think you mean the latter. You just need to keep track of something so that you know which is the "last". Here's a very simple version: https://3v4l.org/oZ7JA – Chris Haas Mar 28 '22 at 17:45
  • Yes, edited my question to be more specific. – twelvell Mar 28 '22 at 18:11
  • This is structured as a _list_ already, so I think you should rather not insert the commas into the content to begin with, but add them via CSS pseudo elements. https://stackoverflow.com/q/1517220/1427878 – CBroe Mar 29 '22 at 09:36

1 Answers1

0

Thanks to @Chris Haas example here https://3v4l.org/oZ7JA I modified my code in this way and now it works as I expected.

<ul class='apt-product-terms'>
    <?php
        $_taxonomies = array(
          'taxname1'   =>   __( 'Taxname1', 'textstringdomain' ),
          'taxname2'   =>   __( 'Taxname2', 'textstringdomain' ),
          'taxname3'   =>   __( 'Taxname3', 'textstringdomain' ),
          'taxname4'   =>   __( 'Taxname4', 'textstringdomain' )
        );
      
      
        foreach ($_taxonomies as $taxonomy_slug => $taxonomy_name) {
            $terms = get_the_terms( $post->ID, $taxonomy_slug);
            if (is_array($terms) && count($terms) > 0) { ?>
               <li class='apt-tax-item'>
                  <span class='apt-term-name'><?php echo $taxonomy_name  ?></span>:&nbsp
                  <ul class='apt-tax-term-list'>
                     <?php $idx = 0; 
                     foreach ( $terms as $term ) { 
                     $idx++; ?>
                     <li class='apt-term-item'>: 
                        <a class='apt-term-link' href="<?php echo get_term_link($term); ?>"> <?php echo $term->name;
    if($idx < count($terms)){
        echo ',&nbsp';
    } ?></a>           
                     </li>
                     <?php } ?>
                  </ul>
               </li>
            <?php            
            }
        }  
    ?>
</ul>
twelvell
  • 257
  • 1
  • 8
  • 19