2

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

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 );
1
  • 1
    I was using jetengine, but I tried ACF and with your tip it was super easy. Thank you very much! May 13 at 6:11
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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