acctman
acctman

Reputation: 4349

ACF retrieving checkbox value from a Taxonomy (category) page

With ACF I understand how to retrieve the checkbox values from a page but unclear from a taxonomy page, here is what I have so far

I have a new ACF which adds a checkboxes called "location"

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

this is my current code which displays the zone name

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

CF settings

enter image description here

Upvotes: 1

Views: 2071

Answers (1)

disinfor
disinfor

Reputation: 11558

To get an ACF field value from a taxonomy term, you need to specify the taxonomy name and the id of the term instead of just an id:

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

In your case, you would do this:

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

Or you can pass the WP_Term Object:

get_field( 'location', $line );

EDIT

If you need to test for just 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.

Upvotes: 1

Related Questions