ACF 从分类法(类别)页面检索复选框值

ACF retrieving checkbox value from a Taxonomy (category) page

使用 ACF,我了解如何从页面检索复选框值,但不清楚如何从分类页面检索复选框值,这是我目前所了解的

我有一个新的 ACF,它添加了一个名为 "location"

的复选框
ACF Field Name = "location"
Choices = 
zone1 : Zone 1
zone2 : Zone 2
zone3 : Zone 3
zone4 : Zone 4

这是我当前显示区域名称的代码

    <div class="selection">
    <?php
        $lines = get_terms( array(
            'taxonomy' => 'zone_line',
            'hierarchical' => true,
            'parent' => 0,
            'hide_empty' => True,
        ) );

        foreach( $lines as $line ):
    ?>


<!-- //start: only show if checkbox zone1 is check -->    
    <a href="<?php echo esc_attr( Center()->link_res_line( $line ) ); ?>">
<?php echo $line->name; ?><span class="fa fa-chevron-right">&nbsp;</span></a>
<!-- //end -->    

        <?php endforeach; ?>
    </div>

要从分类术语中获取 ACF 字段值,您需要指定分类名称和术语的 ID,而不仅仅是一个 ID:

get_field( 'field_name', 'taxonomyname_' . term_term_id )

在你的情况下,你会这样做:

get_field( 'location', 'zone_line_' . $line->term_id );

或者您可以传递 WP_Term 对象:

get_field( 'location', $line );

编辑

如果您只需要测试 zone1:

<div class="selection">
    <?php
    $lines = get_terms( [
        'taxonomy'     => 'zone_line',
        // Added false to hide_empty in case no posts have this term
        'hide_empty'   => FALSE,
    ] );

    foreach ( $lines as $line ):
        $zone = get_field( 'location', $line );

        if ( in_array('zone1', $zone, true ) ) :
            ?>

            <a href="<?php echo esc_attr( Center()->link_res_line( $line ) ); ?>">
                <?php echo $line->name; ?><span class="fa fa-chevron-right">&nbsp;</span></a>

        <?php endif; ?>
    <?php endforeach; ?>
</div>

I tested this in my environment and it works as expected.