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; ?>;">

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");
  • Thanks for this Daniel - Adding either of those to the top of my page just crashes the page unfortunately - Can I ask what the [0] is there for? Each of the products will only have one category :) Jul 10, 2020 at 13:35
  • get_the_terms() returns and array. So [0] grabs the first item off the array. If you turn on error reporting what error does the page show? Jul 10, 2020 at 13:37
  • If I try your first code block it says there is an unexpected '=' but if I try your second block of code it says: Fatal error: Uncaught ArgumentCountError: Too few arguments to function get_the_terms(), 1 passed in /var/www/vhosts/staging.acquiredigital.com/httpdocs/wp-content/themes/acquire-child/single-products.php on line 17 and exactly 2 expected in /var/www/vhosts/staging.acquiredigital.com/httpdocs/wp/wp-includes/category-template.php:1236 - Jul 10, 2020 at 14:05
  • This is after removing every other bit of content from the page so I could see where the error is... Jul 10, 2020 at 14:05
  • 1
    Nice! Add your solution as an answer! Jul 10, 2020 at 15:48

Managed to get it working with the following code:

$terms = wp_get_object_terms($post->ID, 'product_category');
  foreach($terms as $term){
    $exampleName = $term->name;
    $exampleSlugs[] = $term->slug;

    $category_colour_primary = get_field('category_colour_primary', $term);
    $category_colour_secondary = get_field('category_colour_secondary', $term);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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