Support

Account

Home Forums Front-end Issues Loop to display acf image of taxonomy term

Solved

Loop to display acf image of taxonomy term

  • Hi there,

    Im creating a loop to display a list of my custom taxonomy terms, and i have a acf image field attached to my taxonomy.

    Below is what what i currently have, but im struggling to get the term ID into my $image variable. The query works fine when i tested manually by hardcoding a id number in that get_field, but cant seem to get the term id.

    Probably simple, but appreciate the help!

    <?php 
    		$tax = 'product_category';
    		
    		$terms = get_terms( $tax, $args = array(
    		  'hide_empty' => false, // do not hide empty terms
    		));
    		
    		
    		foreach( $terms as $term ) {
    		
    			$term_link = get_term_link( $term );
    			$image = get_field('category_thumbnail', 'product_category_' . $term_id );
    		
    			if( $term->count > 0 ) {
    			   echo '<a href="' . esc_url( $term_link ) . '">';
    				echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';       
    				echo $term->name .'</a>';
    		
    			} elseif( $term->count !== 0 ) {
    				echo '' . $term->name .'';
    				
    			}
    		}
    				
    ?>
  • The first thing you should do is check how you’re calling get_terms(). This changed in WP 4.5 https://developer.wordpress.org/reference/functions/get_terms/

    Once you’re sure your getting the right thing the line for getting the image should look something like this

    
    $image = get_field('category_thumbnail', 'product_category_' . $term->term_id );
    
  • Thanks @hube2 , thats done the trick!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.