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().'/abr';
            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().'/abr';
            endwhile;
         }
        wp_reset_postdata(); // reset global $post;
    }

}

Topic custom-taxonomy custom-field Wordpress

Category Web


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 );

The thumbnail is probably stored in the termmeta table using a meta key specified the jetengine plugin, which is out of scope here.

You should be able to identify the key by querying the DB for one of the category terms. Assuming one of the category terms has and ID of 13 and the default WP table prefix is used, you can query the termmeta like this:

SELECT * FROM wp_termmeta where term_id = 13;

Once you've identified the meta key that stores the image, you can use WP's get_term_meta() function to get the meta value containing the image. Then it's just a matter of outputting the value in an image tag.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.