繁体   English   中英

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

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

我正在 Wordpress 建立一个网站,我有一个名为“旅行”的自定义帖子类型和一个名为“收藏”的自定义分类法。 在分类集合中有不同的术语,例如“集合 1”、“集合 2”、“集合 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 } ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM