dslh32311 2018-01-01 08:16
浏览 20
已采纳

从分类页面检索值

I'm using ACF and I have a color picker field the_field('marker) in taxonomy page project-cat to let client to select color for each category. I want to display each category color for related posts as well.

This code shows how I display color for each category which is fine:

 <?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
                      }

                      ?>

And this is my array for posts :

  <?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']),
              );
       }
          ?>

This code displays one of my category colors for all posts. eg: category A , selected color is pink, all posts shows pink color.

if anyone could help me with correcting the query would be appreciated.

  • 写回答

1条回答 默认 最新

  • duanpasi6287 2018-01-01 11:51
    关注

    I'd go for something like this, having a default color (in my example #ff0000) and then overriding that with the color of the first category of the post. Notice that I'm using wp_get_post_terms - I suppose that is what you want, not get_terms (which will always get ALL the terms, not just those that are selected for the individual 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']),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看