从分类页面检索值

retrieve value from taxonomy page

我正在使用 ACF,并且我在分类页面 project-cat 中有一个颜色选择器字段 the_field('marker),让客户可以为每个类别选择 select 颜色。我也想显示相关帖子的每个类别颜色。

这段代码展示了我如何为每个类别显示颜色,这很好:

 <?php

                  $terms = get_terms( 'project-cat' );
                // var_dump($terms);

                  if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
                      foreach ( $terms as $term ) {


                          ?>
                              <li>
                                <input id="category_<?php echo $term->term_id; ?>" type="checkbox" name="categories" value="<?php echo $term->term_id; ?>" checked="checked" disabled>
                                <label for="category_<?php echo $term->term_id; ?>">
                                    <div class="catCircle" style="background-color:<?php $marker = get_field('marker', $term->taxonomy . '_' . $term->term_id); echo $marker; ?>"></div>
                                    <?php echo $term->name; ?>
                                </label>
                              </li>

                          <?php
                      }

                      ?>
                    <?php
                      }

                      ?>

这是我的帖子数组:

  <?php 


       $args = array(
          'post_type'      => 'project',
          'posts_per_page' => -1,


        );


      $posts_array = get_posts($args); 
       foreach($posts_array as $post){
        $post=(array)$post;
        $location = get_field('google_map',$post['ID']);
     $term = get_terms( 'project-cat' );

          $array[] = array(
                          'title' => $post['post_title'],
                          'subtitle' => get_field('status',$post['ID']),
                          'catColor' => get_field('marker', $term->taxonomy . '_' . $term->term_id),
                          'catID' => get_field('taxo',$post['ID']),
                          'lat' => $location['lat'],
                          'lng' => $location['lng'],
                          'url' =>get_permalink($post['ID']),
              );
       }
          ?>

此代码为所有帖子显示我的类别颜色之一。 例如:类别 A,selected 颜色为粉红色,所有帖子均显示粉红色。

如果有人可以帮助我更正查询,我们将不胜感激。

我会选择这样的东西,使用默认颜色(在我的示例中为 #ff0000),然后用 post 的第一个类别的颜色覆盖它。请注意,我使用的是 wp_get_post_terms - 我想这就是您想要的,而不是 get_terms(它将始终获得所有条款,而不仅仅是为个人选择的条款 post) .

$terms = wp_get_post_terms( $post['ID'], 'project-cat' );
$catColor = "#ff0000";
if(is_array($terms) && count($terms)) {
    if($newColor = get_field('marker', $terms[0]->taxonomy . '_' . $terms[0]->term_id)) {
        $catColor = $newColor;
    }
}
$array[] = array(
    'title' => $post['post_title'],
    'subtitle' => get_field('status',$post['ID']),
    'catColor' => $catColor,
    'catID' => get_field('taxo',$post['ID']),
    'lat' => $location['lat'],
    'lng' => $location['lng'],
    'url' =>get_permalink($post['ID']),
);