简体   繁体   中英

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="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;?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM