0

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>';
            }
        }
     ?>
2
  • I think it will help with understanding if you add some screenshots. Thanks
    – alhoseany
    Commented Jul 3, 2021 at 11:39
  • Will do next time, Sohan had me sorted very quickly however.
    – tch
    Commented Jul 9, 2021 at 17:13

1 Answer 1

0

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.

4
  • Ah I see, thank you @Sohan! The second example was what I was going for, I've given this a go and, interesting, whilst it does seem to implement isotope exactly how I was hoping, it also displays a php error: . ( ! ) Warning: Invalid argument supplied for foreach() in /Users/t/Local Sites/nowlistenhere/app/public/wp-content/themes/_listenhere-v2/page-shows.php on line 70 . Line 70 is foreach( $topic_terms as $topic_term ) { $classes[] = $topic_term->slug; }
    – tch
    Commented Jun 30, 2021 at 11:41
  • Ignore my last, that error may be due to a limitation in my local environment. I tried your code on my live instance just to see.... and it appears to work perfectly!
    – tch
    Commented Jun 30, 2021 at 12:19
  • Glad that helped. Most probably on local environment you have debugging on and on live it's off. Anyway, maybe on some terms, the field value is not set, so you are getting FASE instead of array. Doing a check with condition should solve it. Check my updated code. Commented Jul 1, 2021 at 12:09
  • Superb, you're right it did seem to be that., updated code got it fixed. Thank you so much for your help, really appreciate it!
    – tch
    Commented Jul 1, 2021 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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