0

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.

4
  • In the statement $parent = $term->parent, variable $parent wouldn't be an object but integer ( the parent ID ). So, you would just write out echo $parent;
    – Howdy_McGee
    Apr 18, 2016 at 20:35
  • Yes you are correct, OK so i've just inserted $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';
    – Adrian
    Apr 18, 2016 at 20:38
  • 1
    actually it's just easier to give the div categoryblock a second class with the id in and use CSS to amend such as echo '<div class="categoryblock block' . $parent . '">';
    – Adrian
    Apr 18, 2016 at 20:45
  • 1
    That will work just as well!
    – Howdy_McGee
    Apr 18, 2016 at 20:47

1 Answer 1

0

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.

1
  • 1
    Whilst this is not the solution i've gone for, it's the same sort of concept i my other comment so I thank you for your time, appreciate your help.
    – Adrian
    Apr 18, 2016 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.