ACF-获取自定义分类术语图像字段

时间:2021-05-04 作者:Juraj

我有一个显示自定义分类法中所有术语的循环。我还有图像字段,附加到自定义分类法。现在,我想在我的页面模板中显示图像:

<?php
/* Template Name: Služby */
  
get_header();
?>

<section id="sluzby" class="section--padding" style="background: #eff1f5;">
    <div class="container">
        <div class="row">

            <?php       
            $terms = get_terms( array( \'taxonomy\' => \'sluzba\', \'hide_empty\' => false ) );
            $term_image = get_field( \'tax_image\' );
                    
            foreach ( $terms as $term ) : ?>
            
                <div class="col-lg-4 col-md-6">
                    <div class="product product-zoom product--card">
                        <div class="product__thumbnail">

                            <?php //if ( $term_image ) : ?>

                                <img src="<?php echo $term_image[\'url\']; ?>" alt="Služba <?php echo $term->name; ?>">

                            <?php //endif; ?>

                        </div>

                        <div class="product-desc">
                            <h4 class="text-center mb-2"><?php echo $term->name; ?></h4>
                        </div>
                    </div>
                </div>              
            
            <?php
            endforeach
            ?>
            
        </div>
    </div>
</section>

<?php
get_footer();

enter image description here

1 个回复
最合适的回答,由SO网友:Juraj 整理而成

我通过这个例子做到了:

https://support.advancedcustomfields.com/forums/topic/loop-to-display-acf-image-of-taxonomy-term/

<?php       
$terms = get_terms( array( \'taxonomy\' => \'sluzba\', \'hide_empty\' => false ) );

foreach ( $terms as $term ) : 
    $term_image = get_field( \'tax_image\', \'sluzba_\' . $term->term_id ); ?>

    <div class="col-lg-4 col-md-6">
        <div class="product product-zoom product--card">
            <div class="product__thumbnail">

                <?php if ( $term_image ) : ?>

                    <img src="<?php echo $term_image[\'url\']; ?>" alt="Služba <?php echo $term->name; ?>">

                <?php endif; ?>

            </div>

            <div class="product-desc">
                <h4 class="text-center mb-2"><?php echo $term->name; ?></h4>
            </div>
        </div>
    </div>              

<?php
endforeach
?>

相关推荐