I found the solution myself, thanks however for the attention! This is what worked for me:
$field = get_field('showmap');
if($field == '1'){
echo 'content';
}else{
echo 'no content';
}
Thank you, getting closer. I had a parse error with the code you provided and changed it slightly to that:
<?php
$args = array(
'taxonomy' => 'reisezeit'
);
$terms = get_terms($args);
$selected_terms = get_field('saison', false, false); // get only term IDs
if ($selected_terms) {
foreach ($terms as $term) {
if (in_array($term->term_id, $selected_terms)) {
echo 'Found';
}
else {
echo 'NotFound';
}
}
}
?>
This returns me: FoundFoundNotFoundNotFoundFoundFoundFoundFoundFoundFoundFoundFoundFoundFoundNotFound
which is a little bit strange since it’s 15 matches/non matches, but my taxonomy can have 16 values (12 months + 4 seasons). Querying like so:
<?php
$args = array(
'taxonomy' => 'reisezeit'
);
$terms = get_terms($args);
$selected_terms = get_field('saison', false, false);
print_r ($selected_terms);
?>
Gives me the ID’s of the selected terms:
Array ( [0] => 105 [1] => 95 [2] => 96 [3] => 97 [4] => 106 [5] => 98 [6] => 99 [7] => 100 [8] => 101 [9] => 107 [10] => 102 [11] => 103 )
which should be fine.
But how can I query in this result for a specific term ID? (as an end result I want to have 12 divs for all 12 months, but those months that are not selected greyed out // the selected ones marked with a color). So I need to do 12 in_array queries.
Thank you, Ralf
Hm. The above code returns an array of all terms. I also tried out get_field_object which extends the array returning an array of all fields, labels, terms etc. I can’t see how the get_terms function would give a different result?
However how would I look over all terms?
I tried to compare using PHP in_array function but that didn’t work (it’s always giving me “true” no matter if the term is set or not).
Any idea on how to write that loop?
Thank you, Ralf
It’s only returning one specific term. I’ll try that out. Thank you very much!
However, in that minute I found a solution that was too simple … I named my graphics exactly like the term names and queried the term name to complete the URL. Like so:
/img/tour-badges/<?php $term = get_field(‘tourcharakter’); if( $term ): ?><?php echo $term->name; ?><?php endif; ?>.png”>
The result is URL/term1.png // URL/term2.png etc. Sometimes you don’t manage to see the forest for all the trees that are in the way (like we say in Germany 😉
Thanks again!
Thank you, but that’s not suitable. I’m doing a relaunch of a site that has already 300+ posts and in that case I would have to reopen them all. That’s what I would like to avoid. The taxonomy for instance is used for filtering purposes only. I hope I could use it for that purpose as well (I have another case like that in my website and finding the solution, I could get rid of 4-5 fields).
Also I made an error since it’s not an Icon in the meaning of “Icon font” but a graphic which has a distinct URL. So the logic would be:
If value is 1 then load div with url-to-graphic 1
else
If value is 2 then load div with url-to-graphic 2
etc.
Ah, the values are in plain text.
Oh, sorry. It’s field type “taxonomy”
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.