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
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.