Home > Back-end >  Get ACF Image field from Custom Taxonomy loop/list (woocommerce)
Get ACF Image field from Custom Taxonomy loop/list (woocommerce)

Time:11-08

Been banging my head for a few hours trying to sort this. Finally got a function together that will list all the terms in a custom taxonomy I created in Woocommerce > Products, which works.

What I want to do:

Get/Display the image for each term next to the title (Created an Image field with ACF for that taxonomy item) and display the description underneath.

I can probs figure out the description part, but having a hard time with getting the image to render.

Here's what I've got so far:

//---------Start ACF code
//
// Define taxonomy prefix
// Replace NULL with the name of the taxonomy eg 'category'
$taxonomy_prefix = 'item';

// Define term ID
// Replace NULL with ID of term to be queried eg '123' 
$term_id = NULL;

// Define prefixed term ID
$term_id_prefixed = $taxonomy_prefix .'_'. $term_id;

//----------End ACF Code 

$taxonomy     = 'item';
  $orderby      = 'name';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 0;

  $args = array(
         'taxonomy'     => $taxonomy,
         'orderby'      => $orderby,
         'show_count'   => $show_count,
         'pad_counts'   => $pad_counts,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
 $all_categories = get_categories( $args );
 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id; 
        $image = get_field('image', $taxonomy . '_' . $term_id);
        echo '  <img src="'.the_field( 'image', $term_id ) .'" /> ';
        echo '<a href="'. get_term_link($cat->slug, 'item') .'">'. $cat->name .'</a><br />';

        $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo  '<a  href="'. get_term_link($sub_category->slug, 'item') .'">'. $sub_category->name .'</a><br/>';
                }
            }
    }       
}

You can see the output here: https://doorsdev.wpengine.com/individual-flower-essence-descriptions/

When I inspect I can see the field rendering but a blank src attribute, and with no errors on the page, I'm not sure what to do next.

I'm guessing it's something to do with the $term_id, becuase it tells me to define it, but since I don't want one specific term, I want it for each term that gets pulled into the loop....I'm not sure how to define that.

Any help appreciated.

Thanks!

CodePudding user response:

as shown in the code your $term_id is null. Try replacing $term_id with $category_id.

Replace this code(EDITED).

 $image = get_field('image', 'term_' . $category_id);
 echo '  <img src="'.$image .'" /> ';

Reference: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

  • Related