Echo ACF taxonomy field within a foreach for another taxonomy

I am echoing thumbnails for a list of items within a ‘Show’ custom taxonomy. Within this taxonomy I have additional fields via Advanced Custom Fields / ACF:

'podcast_category_thumb' = image field
'podcast_topic' = Taxonomy field linked to another custom taxonomy.

Using the code below I can output the image just fine. But where I have

echo get_field('podcast_topic', $term);

“I simply receive “ARRAY”.

I guess it’s because it’s a taxonomy field with multiple values(?) What I’m hoping for is just an echo of the ‘podcast_topic’ content. I’ve googled this like crazy but I can’t seem to find a way to achieve this without a foreach to list each item, but since I’m already within a foreach there doesn’t seem to be any way to do it.

Am I daft and/or missing something obvious, or is there a better way to handle this??

    <?php 
        if( get_terms('shows') )
        {
            foreach( get_terms('shows') as $term )
            {
                    echo '<div class="item ';
                    echo get_field('podcast_topic', $term);
                    echo ' col-4 col-md-3 col-lg-2"><a href="/shows/';
                    echo $term->slug;
                    echo '">';
                    echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false);
                    echo '</a></div>';
            }
        }
     ?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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
        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>';
    }
}
?>

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x