显示自定义帖子类型类别

时间:2017-10-01 10:10:28

标签: php wordpress custom-post-type

我正在尝试显示我已添加到自定义帖子类型类别(分类法)的字段。分类标准称为" category-products"。我添加了一个名为" 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;?>

1 个答案:

答案 0 :(得分:0)

如果您阅读此处的文档: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 );