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

时间:2018-06-15 11:13:57

标签: php wordpress custom-post-type custom-taxonomy

我正在使用Wordpress的WP Job Manager插件,它基本上是基于自定义帖子类型构建的。它已启用类别,我已为每个类别指定了颜色。

我可以使用类似的东西来显示颜色:

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

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

我可以使用以下方法查看类别列表:

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

所以,我认为我不得不将两者结合起来 - 这就是我被卡住的地方......

它基本上需要说;

&#39;获取类别&#39;

&#39;获得该类别的颜色&#39;

&#39;将该颜色作为背景颜色应用于div&#39;

我尝试了以下各种格式:

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

但我似乎无法正常工作。任何帮助或建议将受到大力赞赏 - 感谢您的期待! :)

1 个答案:

答案 0 :(得分:1)

请检查以下代码。

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