从自定义邮政类型类别中显示ACF现场值



我正在尝试显示我添加到自定义邮政类型类别(分类法)的字段。分类法称为"类别产品"。我添加了一个名为" category_image"的字段,其中我想添加一个图像。但是ACF字段没有显示该值。这是我到目前为止尝试过的。

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

如果您在此处阅读文档:https://www.advancedcustomfields.com/resources/the_field/,您应该将帖子ID作为the_field()函数的第二个参数传递。例如

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

注意post_id$format_value参数都是可选的。

使用术语,我认为您需要将术语名称和术语ID传递为第二个参数,例如

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

最新更新