最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Show ACF field from custom taxonomy and display on the single template

programmeradmin7浏览0评论

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!

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!

Share Improve this question asked Jul 10, 2020 at 11:47 Shaun TaylorShaun Taylor 1611 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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");

Managed to get it working with the following code:

<?php
$terms = wp_get_object_terms($post->ID, 'product_category');
if(!empty($terms)){
  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);

  }
}
?>
发布评论

评论列表(0)

  1. 暂无评论