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="https://wordpress.stackexchange.com/questions/139940/<?php echo $portfolio_image["sizes']['portfolio-thumbnail']; ?>" alt="https://wordpress.stackexchange.com/questions/139940/<?php echo $portfolio_image["alt']; ?>" />
<div class="mask">
<a class="mask-link" href="https://wordpress.stackexchange.com/questions/139940/<?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; ?>