Home › Forums › Front-end Issues › Get taxonomy list from acf value
I have a custom taxonomy called “shops”. I have some custom fields related to the taxonomy. 1 custom field is a checkbox with 3 options: option 1, option 2 option 3
on my front page i want to show a list of the taxonomy shops only if the value of the custom field is option 1.
is this possible? If it is possible how do i do that?
the code i tried is:
<?php
$terms = get_terms("shops");
$value = get_field('shop_options', 'shops_'.$wp_query->queried_object->term_id);
if( $value == "option_1" )
{
echo 'something';
}
?>
Hi @Ron Willemse
The get_terms function will return an array of terms. This means you need to loop through the code and run your if statement for each term like so:
<?php
$terms = get_terms("shops");
if( $terms )
{
foreach( $terms as $term )
{
$value = get_field('shop_options', 'shops_'.$term->term_id);
if( $value == "option_1" )
{
echo 'something';
}
}
}
?>
Hope that helps
Thanks
E
Thnx for the advice
if i change
$value = get_field(‘shop_options’, ‘shops_’.$term->term_id);
to
$value = implode(‘, ‘, get_field(‘shop_options’, ‘shops_’.$term->term_id));
it works
only now i get the error Warning: implode(): Invalid arguments passed in (path) on line 6 .
do i have to put something else in the code?
Hi @Ron Willemse
You most likely need to test that the value exists before passing it into the implode function.
Thanks
E
i can’t figured out what to add to the code if i want to check if the value exists. can you help me?
Hi @Ron Willemse
This is called an if statement. Please review the ACF documentation code examples or refer to Google / PHP for documentation about the if statement.
Thanks
E
i know its a if statement but i don’t know where to put it. I’m not a php developer.
i tried:
if ($value){
$value = implode(', ', get_field('winkel_uitgelicht', 'winkels_'.$term->term_id));
}
but that don’t work
i found a solution:
if (is_array(get_field('winkel_uitgelicht', 'winkels_'.$term->term_id))) {
$value = implode(', ', get_field('winkel_uitgelicht', 'winkels_'.$term->term_id));
The topic ‘Get taxonomy list from acf value’ is closed to new replies.
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.