Home › Forums › General Issues › How can I apply an ACF field to a single custom post if it refers to the taxomy
I need to hide a div inside custom posts in a given category. I created an ACF True/False field and applied it to the category taxonomy for this post type. But this True/false should work on internal records that refer to the selected category to which ACF True/false is applied.
I tried something like this, but it didn’t work for me
<?php
$category = get_the_terms($id, 'product_cat');
$cater = $category[0]->term_id;
$taxonomy = 'product_cat';
?>
<?php if ( 'yes' == get_field( 'disable_finger', $taxonomy . '_' . $cater )): ?>
<?php echo 'none';?>
<?php else: ?>
<div class="finger-size-wrap">
<select class="finger-size">
<option value="" selected="selected">Select finger size</option>
<?php for($i=3; $i<=10; $i=$i+0.5){
echo '<option value="'.$i.'">'.$i.'</option>';
}?>
</select>
</div>
<?php endif; ?>
SCREENS :
https://prnt.sc/5AE-OO9UOWWw
https://prnt.sc/iqmDFypaZAAm
The post ID when getting values from a taxonomy should be 'term_'.$term->id
A true false field returns either true or false, not yes or no.
if (get_field( 'disable_finger', 'term_' . $cater )
// field is set to true
I tried like this but it didn’t work for me 🙁
<?php
$category = get_the_terms($id, 'product_cat');
$cater = $category[0]->term_id;
//$taxonomy = 'product_cat';
?>
<?php
if ( get_field( 'disable_finger', 'product_cat_' . $cater )):
?>
<?php echo 'none';?>
<?php else: ?>
<div class="finger-size-wrap">
<select class="finger-size">
<option value="" selected="selected">Select finger size</option>
<?php for($i=3; $i<=10; $i=$i+0.5){
echo '<option value="'.$i.'">'.$i.'</option>';
}?>
</select>
</div>
<?php endif; ?>
I seem to understand what the problem is. my $cater returns not the id of the category, but something else, some number I don’t understand.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.