Display all taxonomy terms, add class if term applies to current post
-
22-04-2021 - |
Frage
I have a custom taxonomy for a custom post type. I need all available terms (and their descriptions) to display on a single post from this post type, regardless of if each term is applied to it. Basically if a post has the term it will be bold and if it doesn't have the term it will be greyed out but visible. I was able to get it to work with conditional statements for each term, but I want the client to be able to add or remove terms in the future without my having to update the template each time.
Lösung
I was able to put this together from a similar question I found. If anyone else needs it:
<?php
$terms = get_terms( array(
'taxonomy' => 'features',
'hide_empty' => false
) );
if (!empty($terms) && ! is_wp_error( $terms )) {
echo '<section>';
foreach ($terms as $term) {
$class = has_term( $term->term_id, 'features' ) ? 'active' : 'unassigned';
echo '<div class="term ' . $class . '"><h6>' . $term->name . '</h6>';
echo '<div class="availability"></div>';
echo '<p>'.$term->description.'</p></div>';
}
echo '</section>';
} ?>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange