显示多个 Woocommerce 自定义分类法的术语

我需要来自三个自定义 Woocommerce 分类的术语的内爆列表,包括要在产品循环中显示的分类名称。我还需要能够为每个分类显示多个术语。但是,我只能让它与一种分类法一起工作。而且我不知道如何显示相应的分类名称。


我的自定义分类法是“艺术家”、“插画家”和“作者”。这是我要完成的工作:


罗伯特·道格拉斯(作者)、比尔·约翰斯顿(插画家)、凯尔·麦克贝斯(艺术家)


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>';

    }

}


胡说叔叔
浏览 155回答 1
1回答

一只萌萌小番薯

添加以下代码片段以完成您的任务 -add_action( 'woocommerce_after_shop_loop_item', 'list_author_terms', 6 );function list_author_terms(){&nbsp; &nbsp; $taxonomies = array( 'authors', 'artists', 'illustrators' );&nbsp; &nbsp; $pro_list_terms = array();&nbsp; &nbsp; foreach ( $taxonomies as $taxonomy ) {&nbsp; &nbsp; &nbsp; &nbsp; $term_obj_list = get_the_terms( get_the_ID(), $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; $tax_obj = get_taxonomy( $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; if( $term_obj_list && ! is_wp_error( $term_obj_list ) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $term_obj_list as $term ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $link = get_term_link( $term, $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pro_list_terms[] = '<a href="' . esc_url( $link ) . '" class="author rsc-tp">' . $term->name . ' (' .$tax_obj->labels->singular_name . ')</a>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; echo join( ', ', $pro_list_terms );}
打开App,查看更多内容
随时随地看视频慕课网APP