wordpress foreach循环中的ACF图像字段

qrjkbowd  于 2023-05-06  发布在  WordPress
关注(0)|答案(1)|浏览(149)

我想在这个循环中加载我的ACF字段cat_img,它显示为$cat->image。我使用的现有代码块是:

<?php 
      $cats = get_categories($image = get_field('cat_img'));
      foreach ($cats as $cat) { ?>
        <div class="col-md-6 col-xl-3 mb-4 mx-auto">
          <div class="bg-image ripple rounded-5" data-mdb-ripple-color="light">
            <img class="w-100" src="<?php $cat->image ?>"
              style="height: 288px; object-fit: cover" />
            <a href="'. get_term_link($cat).'">
              <div class="mask" style="background-color: rgba(255,255,255, 0.5)">
                <div class="d-flex justify-content-center align-items-center h-100">
                  <p class="h4 fw-bold text-uppercase text-dark mb-0"><?php echo $cat->name ?></p>
                </div>
              </div>
              <div class="hover-overlay">
                <div class="mask" style="background-color: rgba(255,255,255, 0.1)"></div>
              </div>
            </a>
          </div>
        </div>
      <?php  ;
      }
      ?>

任何关于如何让它显示自定义字段的帮助都会很棒。谢谢你的帮助!

5ssjco0h

5ssjco0h1#

我在forEach循环中漏掉了这一行:

$image = get_field('cat_img', 'category_' . $cat->term_id );

这让我现在可以运行这段代码来获取ACF字段图像作为类别名称后面的平铺(您将看到调用图像的<? echo $image['url']?>):

<?php
      
      $cats = get_categories();
      foreach ($cats as $cat) {
        $image = get_field('cat_img', 'category_' . $cat->term_id );
      ?>
        <div class="col-md-6 col-xl-3 mb-4 mx-auto">
          <div class="bg-image ripple rounded-5" data-mdb-ripple-color="light">
            <img class="w-100" src="<? echo $image['url']?>"
              style="height: 288px; object-fit: cover" alt="<? echo get_field('cat_img', $cat->ID) ?> Category Image"/>
            <a href="<? echo get_term_link($cat);?>">
              <div class="mask" style="background-color: rgba(0,0,0, 0.35)">
                <div class="d-flex justify-content-center align-items-center h-100">
                  <p class="h4 fw-bold text-uppercase text-light mb-0"><?php echo $cat->name ?></p>
                </div>
              </div>
              <div class="hover-overlay">
                <div class="mask" style="background-color: rgba(255,255,255, 0.1)"></div>
              </div>
            </a>
          </div>
        </div>
      <?php  ;
      }
      ?>

相关问题