While this is not specifically WordPress but some general PHP, here’s a solution which you could use:
$terms = get_the_terms( $post->ID , 'prodcats' );
foreach( $terms as $term ) {
$classes="categoryblock";
switch( $term->parent ) {
case 3:
$classes .= ' blue' // Notice the space between the opening string - important for CSS
break;
}
echo '<div class="' . $classes . '">';
echo '<h3>' . $term->name . '</h3>';
echo '</div>';
// unset( $term ); // I'm not sure this works and it would be unnecessary
}
So, we are using a PHP Switch at the top to append our class name to a string. Whenever a $term->parent == 3
PHP will append blue
to the string $classes
so it will look like this: categoryblock blue
which then will get added to the <div/>
class attribute. This is assuming you have a .blue{color:blue}
class in your CSS stylesheet.
On another note, there’s no need to unset anything from your terms array unless you’re going to be doing addition operations with that array later. PHP Foreach will iterate through the array once from top to bottom.