'Wordpress: check whether the current post is in any of the specified terms
I have a custom post type property and a custom taxonomy features. On single property pages, I output all terms from features, including empty ones. However, I need to check which of these terms the current post is present in.
At the exit you need to get something like: ✓ cleaning X Internet ✓ home phone.
So, I get all the terms:
$features = get_terms([
'taxonomy' => 'features',
'hide_empty' => false,
]);
Then I try to sort them out and check if there is a current post in the term being checked.
foreach ($features as $feature) :
if (has_term($feature)) {
echo '✓' . $feature->name;
} else {
echo 'X' . $feature->name;
}
endforeach;
But I get:
X cleaning X Internet X home phone
Solution 1:[1]
Ok, I made a mistake, that's how it will work)
foreach ($features as $feature) :
if (has_term($feature, 'features')) {
echo '?' . $feature->name;
} else {
echo 'X' . $feature->name;
}
endforeach;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergey Pervushin |
