microb14
microb14

Reputation: 483

Custom type - display all taxonomy name and image

I need to display on my single page, each taxonomy name and image. i have 10 different image for taxonomy : 'brique'

It's ok for the name but i can't display image

For the image

<?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 .'';
            
        }
    }
            
            ?>

image 1

image2

Upvotes: 0

Views: 215

Answers (1)

Bhautik
Bhautik

Reputation: 11282

Try the below code. first, you need to pass taxonomy Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array: you can check here get_terms()

in get_field you have pass second parameter as term_id or $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 .'';
        }
    }
            
?>

Upvotes: 1

Related Questions