Search code examples
wordpressadvanced-custom-fieldscustom-taxonomy

Get all taxonomy term and their acf image field


I am very new to wordpress theme development and I have created a custom post and custom taxonomy . I have created a image acf on taxonomy now I want to show all the taxonomy term and their acf fields on front-page . I can get all term by using get_terms() function but I don't know how to get acf field of that taxonomy.

$terms = get_terms(array(
"taxonomy" => "categories",
"hide_empty" => false ));
foreach($terms as $term): 
echo $term->name;
endforeach; 

I want term name and image acf field of that term on front-page.php . Any advice will be helpful and thanks in advance.


Solution

  • Please try this code

    <?php
    
    $terms = get_the_terms(get_the_ID(), "categories");
    
    if (!empty($terms)): ?>
        <ul>    
            <?php foreach ($terms as $term): ?>
                <li class="<?php echo $term->slug; ?>">
                    <img src="<?php the_field("image_field_name", $term); ?>" />
                </li>
            <?php endforeach; ?>
        </ul> 
    <?php endif;
    ?>
    

    For more details you can check this documentation.