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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题