0

I have loop that displays all terms from custom taxonomy. I also have image field, attached to custom taxonomy. Now, I want to display image in my page template:

<?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
  • 2
    try to put $term_image = get_field( 'tax_image' ); inside your foreach loop (because you need to get image for every term) and pass term object inside get_field function, like this - $term_image = get_field( 'tax_image', $term );
    – anton
    Commented May 4, 2021 at 15:29

1 Answer 1

0

I did it by this example:

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
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.