你面临的问题是get_the_terms()
返回array
(或false或aWP_Error
-对象),而不是字符串。
因此,假设您有多个图像术语,可以使用如下代码:
if( $labels && !is_wp_error( $labels ) ) {
foreach( $labels as $label ) {
$labelarray[] = $label->name;
}
$labelstring = join( ", ", $labelarray );
}
如果没有错误,这将在标签中循环。您可能需要调整
$labelstring
根据您的需要,这一个可以产生如下值
label1, label2, label3
. 如果不需要,可以跳过冒号。
您的整个代码如下所示:
<?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; ?>