微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Foreach遍历cpt类别以使用ACF值

如何解决Foreach遍历cpt类别以使用ACF值

我正在遍历CPT类别,并且想在每个循环中使用我的ACF字段。

 <div id="response">
         <?PHP
         $args = array(
          'post_type'  => 'segments-overview','orderby' => 'date',// we will sort posts by date
           'posts_per_page' => -1
        );



        $query = new WP_Query( $args );
        $all_terms = [];
        if( $query->have_posts() ) :
          while( $query->have_posts() ): $query->the_post();

          $terms = get_the_terms(get_the_ID(),'category-segments-overview');
          foreach($terms as $term) $all_terms[$term->term_id] = $term;

          endwhile;

          foreach($terms as $term):
            ?>

          <div class="segments-card">
            <div class="img">image</div>
            <div class="content">
                <div class="title"><?PHP echo $term->name; ?></div>
                
                // ACF field
                <?PHP echo the_field('product',$term->taxonomy) ; ?>

                <a class="button transparent" href="/segments/<?PHP echo $term->slug; ?>">
                <?PHP echo __('View All','axia'); ?>
              </a>
            </div>
            </div>
          </div>

        <?PHP endforeach;

          wp_reset_postdata();
        else :
          ?>
          <div class="no-posts-found">
            <h2>There were no items found</h2>
              <h3>Please try a different search</h3>
          </div>
          <?PHP
        endif;
          ?>
    </div>

谁能告诉我如何在foreach循环中检索我的“产品”字段的值。 现在,我没有收到任何错误,但是我也没有收到任何值。

请帮助。

解决方法

您必须学习有关分类术语的字段的文档: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

根据您的情况,您可以替换

echo the_field('product',$term->taxonomy);

通过(3种可能性)

echo the_field('product',$term);
// or
echo the_field('product',$term->taxonomy . '_' . $term->term_id);
// or
echo the_field('product','term_' . $term->term_id);

如何从特定术语加载字段?

acf的基本加载字段为:get_field('key',$post_id)

对于分类术语,有$post_id的3种不同样式,并在下面列出。 (来自acf文档)

+----------------+----------------------------+-----------------------------------------------------------------------------+
| Example        | Format                     | Description                                                                 |
+----------------+----------------------------+-----------------------------------------------------------------------------+
| 'category_123' | $taxonomy . '_' . $term_id | A string containing the taxonomy name and term ID                           |
+----------------+----------------------------+-----------------------------------------------------------------------------+
| 'term_123'     | 'term_' . $term_id         | 'term_' . $term_id                                                          |
|                |                            | A string containing the word ‘term’ and term ID. Added in version 5.5.0     |
+----------------+----------------------------+-----------------------------------------------------------------------------+
| WP_Term        | $term                      | A term object. You can get a term object via many of WP’s functions such as |
|                |                            |                                                                             |
|                |                            | get_term()                                                                  |
+----------------+----------------------------+-----------------------------------------------------------------------------+

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。