admin管理员组文章数量:1331613
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 badges2 Answers
Reset to default 2I 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);
}
}
?>
本文标签: phpShow ACF field from custom taxonomy and display on the single template
版权声明:本文标题:php - Show ACF field from custom taxonomy and display on the single template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742274490a2444936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论