show ACF field value from custom post type category

i am trying to show a field that i have added to custom post type category (taxonomy). the taxonomy called "category-products". i have added a field called "category_image" in which i want to add an image. But the acf field is not showing the value. here is what I have tried so far.

<?php

$taxonomy = 'category-products';
$terms = get_terms($taxonomy); 

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li class="infoflowing-style">

            <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?>

            <?php the_field('category_image', $terms ); ?>

            </a></li>
        <?php } ?>
    </ul>
<?php endif;?>

Comments


  • Promise

    If you read the documentation here: https://www.advancedcustomfields.com/resources/the_field/, you should be passing the Post ID as the second parameter of your the_field() function. Eg.

    the_field($selector, [$post_id], [$format_value]);

    Noting that the post_id and $format_value parameters are both optional.

    With terms, however, I think you would need to pass the term name and term id as the second parameter eg.

    the_field( 'category_image', $term->name . '_' . $term->term_id );

Add Comment