Support

Account

Home Forums General Issues Category Color in Single Post Reply To: Category Color in Single Post

  • G’day Mate,

    the function get_queried_object is getting the current object. So, on a archive page, the current object is the current taxonomy term. However, on the single, it is the current post. That’s why acf couldn’t return the value unless you tell it to get it from the taxonomy.

    On line 192, where the theme is using get_the_term_list to build the category list. However, if you want to apply custom class to it, you’d need to construct the html output yourself.

    So, you should update that section to look like:

    
    if(!empty($taxonomies))
    {
        foreach($taxonomies as $taxonomy)
        {
            if(!in_array($taxonomy, $excluded_taxonomies))
            {
                // get all the terms in this taxonomy
                $terms = get_the_terms(null, $taxonomy)? : [];
    
                foreach ($terms as $term) {
                    // loop through them, and add the color as style attribute
                    $cats .= sprintf(
                        '<a href="%s" style="color: %s">%s</a>', 
                        get_term_link($term),
                        get_field('color', $term),
                        $term->name
                    );
                }
            }
        }
    }
    

    Cheers