1

I am trying to output a list of terms associated with a custom taxonomy for each image I am pulling from a custom field, like so:

$portfolio_images = get_field('portfolio_gallery');

Each image has standard WP fields associated with it, such as sizes, title, caption, etc. I would like to output the list of terms associated with each image from the media_category taxonomy.

I am using get_the_terms inside another variable but I think something is wrong with my foreach statement. Any help would be appreciated. Full code:

<?php

$portfolio_images = get_field('portfolio_gallery');

foreach( $portfolio_images as $portfolio_image ):

$labels = get_the_terms( $portfolio_image['ID'], 'media_category' );
?>

<li class="gallery-item" data-myorder="<?php  echo $labels;  ?>">
     <img src="<?php echo $portfolio_image['sizes']['portfolio-thumbnail']; ?>" alt="<?php echo $portfolio_image['alt']; ?>" />
     <div class="mask">
            <a class="mask-link" href="<?php echo $portfolio_image['url']; ?>">
                <div class="mask-content">
                        <h4><?php echo $portfolio_image['title']; ?></h4>
                        <p><?php echo $portfolio_image['caption']; ?></p>
                </div>
            </a>
     </div>
</li>

 <?php  endforeach; ?>
2

The problem you are facing is that get_the_terms() returns an array (or false or a WP_Error-Object), not a string.

So, assuming that you have multiple terms for images, you could use a code like this:

if( $labels && !is_wp_error( $labels ) ) {
    foreach( $labels as $label ) {

        $labelarray[] = $label->name;

    }
    $labelstring = join( ", ", $labelarray );
}

This loops through your labels, if no error. You may want to adjust the $labelstring to your needs, this one produces a value like label1, label2, label3. You could simply skip the colon, if you don't need it.

Your whole code would look like this:

<?php

$portfolio_images = get_field('portfolio_gallery');

foreach( $portfolio_images as $portfolio_image ):
    unset( $labelarray );
    $labels = get_the_terms( $portfolio_image['ID'], 'media_category' );
    if( $labels && !is_wp_error( $labels ) ) {
        foreach( $labels as $label ) {

            $labelarray[] = $label->name;

        }
        $labelstring = join( ", ", $labelarray );
    }

?>

    <li class="gallery-item" data-myorder="<?php  echo $labelstring;  ?>">
         <img src="<?php echo $portfolio_image['sizes']['portfolio-thumbnail']; ?>" alt="<?php echo $portfolio_image['alt']; ?>" />
         <div class="mask">
                <a class="mask-link" href="<?php echo $portfolio_image['url']; ?>">
                    <div class="mask-content">
                            <h4><?php echo $portfolio_image['title']; ?></h4>
                            <p><?php echo $portfolio_image['caption']; ?></p>
                    </div>
                </a>
         </div>
    </li>

<?php  endforeach; ?>
  • thank you very much that works great. I am having an issue, however, where each sequential item contains the string from the previous item. For example, here is the output: <li class="gallery-item" data-myorder="Identity"></li> <li class="gallery-item" data-myorder="Identity Print"></li> <li class="gallery-item" data-myorder="Identity Print Print Product"></li> <li class="gallery-item" data-myorder="Identity Print Print Product Product"></li> Each item contains the previous items attributes...is there a problem with my syntax? – itrogers Apr 2 '14 at 16:16
  • 1
    oh, right, you need to unset the $labelarray at the beginning of the foreach. I updated the code. – fischi Apr 2 '14 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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