0
votes

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="cate col-md-2">

            <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;?>
1
You are passing in an invalid parameter into the_field. The second parameter should be the post id, but you are passing in the array of WP_TermsFluffyKitten
can you please give me an example? i am new in ACF, thankst.i.rony

1 Answers

0
votes

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