Support

Account

Home Forums General Issues Category Color

Solving

Category Color

  • Hey,

    I’ve set a ACF named “Category Color” with the following specs (see attach):
    – Field Type: Color Picker
    – Rules: Taxonomy IS EQUAL TO category

    Next, I set #b4151a as the “Category Color” value for a specific category (see attach).

    Lastly in Elementor I selected Background -> ACF Color Picker Field -> Category Color (see attach).

    Unfortunately there specified color is not reproduced in the frontend.

    Any ideas what I’m doing wrong?

    Thank you!

  • Elementor is looking for a field on the post, this field does not exist on the post, it exists on the taxonomy term. Elementor cannot do what you are looking for.

    It this instance elementor does use the ACF function get_field(), this is not the case with all field types. Because of this you can use an acf/format_value filter with a priority of > 10 so that it runs after the built in ACF filter, to alter the value.

    In this filter you would get the terms that the post is assigned to using get_post_terms(). For the post ID your would use get_the_ID(). This will return an array of terms. The you need to loop over this array, event if there is only one, and then get and return the color from the term

    
    $return get_field('category_color', $term);
    
  • Hi,
    I have the same problem, but i’m not sure of to fix it. Could you please help us with the snippet to add? thanks

  • 
    add_filter('acf/format_value/field_name=YOUR_FIELD_NAME_HERE', 'YOUR_FUNCTION_NAME_HERE', 20, 3);
    YOUR_FUNCTION_NAME_HERE($value, $post_id, $field) {
      $terms = wp_get_post_terms($post_id, 'YOUR_TAXONOMY_HERE');
      if (!is_wp_error($terms) && !empty($terms)) {
        foreach ($terms as $term) {
          if (get_field('YOUR_FIELD_NAME', $term)) {
            $value = get_field('YOUR_FIELD_NAME', $term);
          }
        }
      }
      return $value;
    }
    
  • Hi John, thanks for you code! But seems to not work

    function cat_elementor_acf_fixer_picker($value, $post_id, $field) {
      $terms = wp_get_post_terms($post_id, 'product_cat');
      if (!is_wp_error($terms) && !empty($terms)) {
        foreach ($terms as $term) {
          if (get_field('colore_categoria', $term)) {
            $value = get_field('colore_categoria', $term);
          }
        }
      }
      return $value;
    }
    
    add_filter('acf/format_value/field_name=colore_categoria', 'cat_elementor_acf_fixer_picker', 20, 3);
  • That’s my best guess at this, I don’t know why it would not be working.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.