I have a custom post type called products, which I have attached a custom taxonomy called Product Categories.

On each of the Product Categories, I have used Advanced Custom Fields to add fields such as product category colour, logo etc.

Initially, I’m trying to display the Product Category colour as a background colour for all of the products that fall inside that specific Product Category.

I’ve tried using the same method as I used to display the colour on the actual Product Category page (taxonomy-product_category.php), by adding this to the top of the page:

// get the current taxonomy term
$term = get_queried_object();

// vars
$category_colour_primary = get_field('category_colour_primary', $term); 

and then referencing it later on like this:

<div class="container-flex" style="background-color:<?php echo $category_colour_secondary; ?>;">
</div>

This works great on each of the Product Category pages, but doesn’t seem to work on the single page template (single-products.php).

I feel I need to get the name (or terms?!) of the current product’s taxonomy and then display them as usual…

I’ve come to a bit of a dead end and I’m unsure what I need to try next…

Can anyone point me in the right direction? I feel I’ve tried a million things, but can’t quite get my head around it. Thanks for looking!

2 Answers
2

I would try something like this…

// Assume there is only one category.
$product_category = get_the_terms($post, 'product-category')[0];

$cat_fields = get_fields("term_$product_category");

$color = $cat_fields['category_colour_primary'];

You could also do this…

// Assume there is only one category.
$product_category = get_the_terms($post, 'product-category')[0];

$color = get_field('category_colour_primary', "term_$product_category");

Leave a Reply

Your email address will not be published. Required fields are marked *