0

I'm using ACF to add categories color. I display them in the articles loop.

I would like to retrieve the parent category color if the child color is empty. If the parent is also empty, get the primary color.

Here is what I have:

$terms = get_the_terms( get_the_ID(), 'category' );
if ( $terms ) {
  foreach ( $terms as $term ) {
    $cat_id = $term->term_id;
  }
  $cat_color = get_field('category-color', 'category' . '_' . $cat_id);
  if ( empty( $cat_color ) ) {
    $cat_color = // Get the parent category color;
  } else {
    $cat_color = get_field('primary-color', 'option');
  }
  echo '<div class="post_loop-cat" style="background-color: '.$cat_color.';">' . $term->name . '</div>';
}

How can I get the color of the parent category?

0

You would need to either get the color of the direct parent or loop thorugh all ansestors until you find the color.
If no color found get the default and output.

I made a few changes to the structure and added all comments for each step.

Notice that I included two options to achive the same result, choose the one that works for your needs.

// get category terms of the current post
if ($terms = get_the_terms( get_the_ID(),'category')) {
    // prepare the output for sprintf
    // to avoid repeating ourselves we can create a variable to conatin the output
    // %s is a sting placeholder
    $output = '<div class="post_loop-cat" style="background-color: %s;">%s</div>';

    // unless you always have one categpry for every post
    // this will always set $cat_id to the last $term of the loop, always
    // take this into account
    foreach ($terms as $term) {
        $cat_id = $term->term_id;
    }

    // get ACF category color
    $cat_color = get_field('category-color', 'category_' . $cat_id);

    // because we don't know the depth of the category
    // or how high do you want to go to get the parent color
    // we can do either of the following
    // in both options we will first check if $cat color exists

    // option 1
    // using the direct parent only

    if ($cat_color) { // color found, output the html
        echo sprintf($output, $cat_color, $term->name);
    } else {
        // get the parent category color
        $cat_color = get_field('category-color', 'category_' . $term->parent);

        // if not found, set color to default
        if (empty($cat_color)) $cat_color = get_field('primary-color', 'option');

        // output the html
        echo sprintf($output, $cat_color, $term->name);
    }

    // option 2
    // using the category ancestors to find the color
    // we loop each ancestor, once we found a color we stop and output

    if ($cat_color) { // color found, output the html
        echo sprintf($output, $cat_color, $term->name);
    } else {
        // get and loop all ansestors
        foreach (get_ancestors($cat_id, 'category') as $ancestor_id) {
            // get and check category color, if found, break out of the loop
            if ($cat_color = get_field('category-color', 'category_' . $ancestor_id)) break;
        }

        // if not found, set color to default
        if (empty($cat_color)) $cat_color = get_field('primary-color', 'option');

        // output the html
        echo sprintf($output, $cat_color, $term->name);
    }
}

In order to not repeat the output I used a variable that will later be used with sprintf, click the link for more information.
quick description, sprintf creates a formatted string.

1
  • Wonderful option 2! Even better than I expected. Thanks!
    – Yco
    Aug 26 at 15:52
0
$terms = get_the_terms( get_the_ID(), 'category' );
if ( $terms ) {
  foreach ( $terms as $term ) {
    $cat_id = $term->term_id;
    $cat_color = get_field('category-color', 'category' . '_' . $cat_id);
    if ( empty( $cat_color ) && $term->parent != 0) {
        $parent_term = get_term( $term->parent, 'category' );
        $cat_color = get_field('category-color', 'category' . '_' . $parent_term->term_id);
        //if parent also empty then assign default color
        $cat_color =empty( $cat_color ) ? "white" : $cat_color ;
    }else{
        //assign some default color if term is itself parent
        $cat_color = 'white';
    } 
    echo '<div class="post_loop-cat" style="background-color: '.$cat_color.';">' . $term->name . '</div>';
  }
}

It will loop through all terms and check if term has a color then assign it to cat_color variable, if empty then it will check if term has a parent if parent is empty then assign $cat_color some default color (you can keep it empty depending upon your code logic) if parent is not empty it($cat_color) will get color of the parent, if parent color is also empty then it will assign some default color to variable $cat_color.

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