自定义类型 - 显示所有分类名称和图像

Custom type - display all taxonomy name and image

我需要在我的单个页面上显示每个分类名称和图像。 我有 10 个不同的分类图像:'brique'

名字可以,但是我不能显示图片

为了图片

<?php 
    $tax = 'brique';
    
    $terms = get_terms( $tax, $args = array(
      'hide_empty' => false, // do not hide empty terms
    ));
    
    
    foreach( $terms as $term ) {
    
        $term_link = get_term_link( $term );
        $image = get_field('visuel' . $term_id );
    
        if( $term->count > 0 ) {
           echo '<a href="' . esc_url( $term_link ) . '">';
            echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';       
            echo $term->name .'</a>';
    
        } elseif( $term->count !== 0 ) {
            echo '' . $term->name .'';
            
        }
    }
            
            ?>

试试下面的代码。首先,你需要通过 taxonomy Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array: 你可以在这里查看 get_terms()

get_field 中,您已将第二个参数传递为 term_id$term object

<?php 

    $tax = 'brique';
    
    $terms = get_terms( array(
        'taxonomy'   => $tax,
        'hide_empty' => false,
    ) );

    foreach( $terms as $term ) {
    
        $term_link = get_term_link( $term );
        $image     = get_field( 'visuel', $term );
    
        if( $term->count > 0 ) {
            echo '<a href="' . esc_url( $term_link ) . '">';
            echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';       
            echo $term->name .'</a>';
    
        } elseif( $term->count !== 0 ) {
            echo '' . $term->name .'';
        }
    }
            
?>