0
votes

I need an imploded list of terms from three custom Woocommerce taxonomies including the taxonomy name to display in the product loop. I'll also need to be able to display multiple terms for each taxonomy. However, I can only get it to work with one taxonomy. And I can't figure out how to display the corresponding taxonomy name.

My custom taxonomies are 'artists', 'illustrators' and 'authors'. Here's what I'm trying to accomplish:

Robert Douglas (Author), Bill Johnston (Illustrator), Kyle McBeth (Artist)

function list_author_terms() {
    global $post;
    $person = get_the_terms(get_the_ID(), 'authors', 'artists', 'illustrators');
    if (    $person
     && !is_wp_error(  $person )
    ) {
    @usort( $person, function ( $a, $b )
    {
    return strcasecmp( 
            $a->slug,
            $b->slug
        );
    });
    // Display your terms as normal
    $term_list = [];
    foreach ( $person as $term ) 
       $term_list[] = '<a href="' . get_term_link( $term ) . '"class="author rsc-tp">' . esc_html( $term->name ) . '<span class="attribute"> (Author)</span> </a>';
       $term_names[] = $term->name;
     echo implode( ', ', $term_list);
        echo '<br>';
    }
}
1

1 Answers

0
votes

Add follows code snippet to achieve your task -

add_action( 'woocommerce_after_shop_loop_item', 'list_author_terms', 6 );
function list_author_terms(){
    $taxonomies = array( 'authors', 'artists', 'illustrators' );
    $pro_list_terms = array();
    foreach ( $taxonomies as $taxonomy ) {
        $term_obj_list = get_the_terms( get_the_ID(), $taxonomy );
        $tax_obj = get_taxonomy( $taxonomy );
        if( $term_obj_list && ! is_wp_error( $term_obj_list ) ){
            foreach ( $term_obj_list as $term ) {
                $link = get_term_link( $term, $taxonomy );
                $pro_list_terms[] = '<a href="' . esc_url( $link ) . '" class="author rsc-tp">' . $term->name . ' (' .$tax_obj->labels->singular_name . ')</a>';
            }
        }
    }
    echo join( ', ', $pro_list_terms );
}