PHP - 获取 CPT 分类法,显示来自 ACF 的分类法颜色

PHP - Get CPT taxonomy, show taxonomy color from ACF

我正在使用适用于 Wordpress 的 WP Job Manager 插件,它基本上是基于自定义 post 类型构建的。它启用了类别,我为每个类别分配了一种颜色。

我可以使用类似以下内容来显示颜色:

<div style="background-color:<?php the_field('job_category_colour', 'category_54'); ?>; height: 100px; width: 100px;"></div>

但这不是很好,因为我将此代码添加到 single.php 页面,所以我需要它足够动态以先检查 post 的类别然后应用post 的颜色。

我可以使用以下方法浏览类别列表:

<?php the_terms( $post->ID, 'job_listing_category'); ?>

所以,我想我必须以某种方式将两者结合起来 - 这就是我遇到困难的地方......

本质上要说;

'get category'

'get color of that category'

'apply that colour as a background color to the div'

我尝试了以下各种格式:

style="background-color:<?php the_field('job_category_colour', '$post->ID, 'job_listing_category''); ?>; height: 100px; width: 100px;"

但我似乎无法使其全部正常工作。任何帮助或建议将不胜感激 - 感谢您的关注! :)

请检查以下代码是否相同。

    $term_list = wp_get_post_terms($post->ID, 'job_listing_category', array("fields" => "all"));
    $currentcolor='';
    foreach($term_list as $term_single) 
    {
        $termid= $term_single->term_id; 
        $colorvalue= get_field('job_category_colour',  'category_' . $termid);
        if($colorvalue!='')  $currentcolor =$colorvalue;
    }


    style="background-color:<?php echo $currentcolor ; ?>; height: 100px; width: 100px;"