Display all taxonomy terms, add class if term applies to current post

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>';  
} ?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x