• Hey all,

    I used the following code from the codex and am having trouble adding a comma to separate the terms. I can add the comma to ‘%2$s’ but then it still shows up on the last item on the list. I’m not sure how to list the terms without the last comma?

    Thanks.

    function wpdocs_custom_taxonomies_terms_links() {
        // Get post by post ID.
        $post = get_post( $post->ID );
     
        // Get post type by post.
        $post_type = $post->post_type;
     
        // Get post type taxonomies.
        $taxonomies = get_object_taxonomies( $post_type, 'objects' );
     
        $out = array();
     
        foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
     
            // Get the terms related to post.
            $terms = get_the_terms( $post->ID, $taxonomy_slug );
            
            if ( ! empty( $terms ) ) {
                $out[] = "<strong>" . $taxonomy->label . ":</strong> ";
                foreach ( $terms as $term ) {
                    $out[] = sprintf( '%2$s', esc_url( get_term_link( $term->slug, $taxonomy_slug ) ), esc_html( $term->name ));
                }
                $out[] = "<br>";
    
            }
        }
          echo implode(" ", $out );
    }
Viewing 1 replies (of 1 total)
  • Hey CyberWoolf,

    Try changing the echo implode(" ", $out ); to echo implode(",", $out );

    That should do the trick.

    Hope this helps

Viewing 1 replies (of 1 total)
  • The topic ‘Taxonomy list with commas’ is closed to new replies.