I have the following working code to echo the name of the term (taxonomy called prodcats
) that a series of posts are in. This is in the taxonomy archive template taxonomy-prodcats.php
$terms = get_the_terms( $post->ID , 'prodcats' );
foreach( $terms as $term ) {
echo '<div class="categoryblock">';
echo '<h3>' . $term->name . '</h3>';
echo '</div>';
unset($term);
}
What I need to do is have the "categoryblock" div a different background color depending on what parent term is being used. There are 5 parent terms in total with IDs 3,4,5,6 and 7.
So, how do I adapt the above to say if this terms parent is 3, then echo x else if terms parent is 4, echo y else if terms parent is 5, echo z etc.. (you get the picture).
I tried several things including $parent = $term->parent; echo $parent->id;
but this didn't output anything.
$parent = $term->parent
, variable$parent
wouldn't be an object but integer ( the parent ID ). So, you would just write outecho $parent
;$parent = $term->parent; echo $parent;
and it returned 3 which is the correct parent ID for what i'm testing this on. So now how would if introduce the conditional part, would it be something like:if($parent == '3') : echo 'do this';
categoryblock
a second class with the id in and use CSS to amend such asecho '<div class="categoryblock block' . $parent . '">';