Skip to main content
Minor code update
Source Link
<?php 
if( get_terms('shows') ) {
    foreach( get_terms('shows') as $term ) {
        $topic_terms = get_field('podcast_topic', $term); // This should be an array of Term Objects
        $classes = []; // Let's store slugs of each term here
        if( is_array($topic_terms) && !empty($topic_terms) ) {
            foreach( $topic_terms as $topic_term ) {
                $classes[] = $topic_term->slug;
            }
        }
        echo '<div class="item ' . esc_attr( implode( ' ', $classes ) ) . ' col-4 col-md-3 col-lg-2">';
        echo '<a href="' . get_term_link( $term ) . '">'; // You were making the term link in wrong way
        echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false);
        echo '</a></div>';
    }
}
?>
<?php 
if( get_terms('shows') ) {
    foreach( get_terms('shows') as $term ) {
        $topic_terms = get_field('podcast_topic', $term); // This should be an array of Term Objects
        $classes = []; // Let's store slugs of each term here
        foreach( $topic_terms as $topic_term ) {
            $classes[] = $topic_term->slug;
        }
        echo '<div class="item ' . esc_attr( implode( ' ', $classes ) ) . ' col-4 col-md-3 col-lg-2">';
        echo '<a href="' . get_term_link( $term ) . '">'; // You were making the term link in wrong way
        echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false);
        echo '</a></div>';
    }
}
?>
<?php 
if( get_terms('shows') ) {
    foreach( get_terms('shows') as $term ) {
        $topic_terms = get_field('podcast_topic', $term); // This should be an array of Term Objects
        $classes = []; // Let's store slugs of each term here
        if( is_array($topic_terms) && !empty($topic_terms) ) {
            foreach( $topic_terms as $topic_term ) {
                $classes[] = $topic_term->slug;
            }
        }
        echo '<div class="item ' . esc_attr( implode( ' ', $classes ) ) . ' col-4 col-md-3 col-lg-2">';
        echo '<a href="' . get_term_link( $term ) . '">'; // You were making the term link in wrong way
        echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false);
        echo '</a></div>';
    }
}
?>
Source Link

First of all, you can do foreach inside foreach. In fact, you can nest loops to much higher level based on your server configuration.

If you are going to use only one term in that field, make sure you select Field Type as Radio or Select and Return Value as Term Object. In that case, your code should look like this:

<?php 
if( get_terms('shows') ) {
    foreach( get_terms('shows') as $term ) {
        $topic_term = get_field('podcast_topic', $term); // This should be a Term Object

        // Optional: you can use $topic_term to get ACF data from that selected term
        $topic_custom_thumb = get_field('podcast_topic_thumb', $topic_term);

        echo '<div class="item ' . esc_attr( $topic_term->slug ) . ' col-4 col-md-3 col-lg-2">';
        echo '<a href="' . get_term_link( $term ) . '">'; // You were making the term link in wrong way
        echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false);
        echo '</a></div>';
    }
}
?>

If you are expecting multiple terms to be selected as podcast topic field, then Field Type should be Multi Select or Checkbox. In that case, you'll get array as value when you use get_field(). Then your code should be something like this:

<?php 
if( get_terms('shows') ) {
    foreach( get_terms('shows') as $term ) {
        $topic_terms = get_field('podcast_topic', $term); // This should be an array of Term Objects
        $classes = []; // Let's store slugs of each term here
        foreach( $topic_terms as $topic_term ) {
            $classes[] = $topic_term->slug;
        }
        echo '<div class="item ' . esc_attr( implode( ' ', $classes ) ) . ' col-4 col-md-3 col-lg-2">';
        echo '<a href="' . get_term_link( $term ) . '">'; // You were making the term link in wrong way
        echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false);
        echo '</a></div>';
    }
}
?>

Remember, when you are getting Term Object from get_field(), you can also use that object to get ACF data from that term. Example in first code block.