How get custom field from custom taxonomy?

I need to list all categories and their respective posts.
Each taxonomy has an image created by ACF in jetengine plugin. I created the loop that returns the list of categories and posts for each category. But I can’t capture the custom field from the taxonomy image. Can anyone give me some tips on how to do it?

foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'post',
            'tax_query' => array(
                array(
                    'taxonomy' => 'custom_tax',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
         );
         $loop = new WP_Query($args);
         if($loop->have_posts()) {
            echo '<h2>'.$custom_term->name.'</h2>';
            while($loop->have_posts()) : $loop->the_post();
                echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
            endwhile;
         }
    }
}

UPDATE:
Following @Abhik tip, tip I was able to list the image of each category using the ACF plugin only for img.

Taxonomy IMG

  • List item (CPT item)
  • List item (CPT item)
  • List item (CPT item)
  • List item (CPT item)

Taxonomy IMG

  • List item (CPT item)
  • List item (CPT item)
  • List item (CPT item)
  • List item (CPT item)

That way I was able to create a really efficient dynamic menu. I appreciate the contribution of those who commented!
I’m still open to suggestions for improvement.

Code:

add_shortcode( 'list_terms_post_cod', 'list_terms_post_func' );
function list_terms_post_func(){
$custom_terms = get_terms('taxonomy');
    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'custom_post',
            'tax_query' => array(
                array(
                    'taxonomy' => 'custom_term',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
         );
         $loop = new WP_Query($args);
         $image = get_field( 'cat_image', $custom_term );

         //Taxonomies Loop
         if($loop->have_posts()) {
            echo '<h2>'.$custom_term->name.'</h2>';
            echo '<img src="'.$image.'"/>';
            
            //Post loop
            while($loop->have_posts()) : $loop->the_post();
                echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
            endwhile;
         }
        wp_reset_postdata(); // reset global $post;
    }

}

2 Answers
2

Since you are using ACF, you can take advantage of the get_field() function ACF provides.

You will need to pass the term object as the second parameter.

$image = get_field( 'field_key', $custom_term );

Leave a Comment