如何获取自定义分类术语的名称、link 和来自 acf 字段的图像

How to get the custom taxonomy terms' names, link and image coming from acf field

我正在 Wordpress 中构建一个网站,我有一个名为 "travels" 的自定义 post 类型和一个名为 "collections" 的自定义分类法。在分类集合中有不同的术语,例如"Collection 1"、"Collection 2"、"Collection 3"等

我想要获得的是一个循环显示所有术语的名称、指向其存档页面和图像的链接(这个来自一个名为 "taxonomy_image" 的 acf 字段)。

所有这些数据都应该在这个结构中检索:

<a href="here goes the link to the term's archive page"><div class="col-3 py-4 coverbackground" style="background:url(here goes the taxonomy image from the acf field); height:300px;">

                <div class="row whiteborders h-100  mx-auto">
                    <div class="col-12 align-self-center">
                        <h2 class="text-center text-white">here goes the term's title</h2>
                    </div>
                </div>




  </div></a>

我希望我已经足够清楚了,因为我是新手,我真的不知道如何处理这个问题。任何类型的建议都将不胜感激。感谢您的帮助

唯一可能不起作用的是图像,因为您必须将 acf 图像字段 Return Value 设置为 Image URL

已测试并正常工作

  <?php
   $terms = get_terms([
      'taxonomy' => 'collections',
  ]);
  foreach($terms as $term) { ?>
  <a href="<?php echo get_term_link( $term ); ?>">
    <div class="col-3 py-4 coverbackground" style="background:url(<?php the_field('taxonomy_image', $term); ?>); height:300px;">
    <div class="row whiteborders h-100  mx-auto">
      <div class="col-12 align-self-center">
          <h2 class="text-center text-white"><?php echo $term->name; ?></h2>
      </div>
    </div>
    </div>
  </a>
  <?php } ?>

如果您使用 img 数组,那么这就是您需要的:

  <?php
   $terms = get_terms([
      'taxonomy' => 'collections',
  ]);
  foreach($terms as $term) {
  $image = get_field('taxonomy_image', $term);?>
  <a href="<?php echo get_term_link( $term ); ?>">
    <div class="col-3 py-4 coverbackground" style="background:url(<?php echo $image['url']; ?>); height:300px;">
    <div class="row whiteborders h-100  mx-auto">
      <div class="col-12 align-self-center">
          <h2 class="text-center text-white"><?php echo $term->name; ?></h2>
      </div>
    </div>
    </div>
  </a>
  <?php } ?>