我创建了名为' product'的高级内容类型POD。 POD具有关系文件字段,用于将图像上传到图库:字段名称为" gallery"。产品在同一产品中相互关联。荚。 我想在前端显示相关领域的图像。 到目前为止,通过将以下代码放入PODS模板,我没有运气:
<?php$related = $obj->field( 'related_products' );
if ( ! empty( $related ) && is_array($related) ) {
foreach ( $related as $rel ) {
$id = $rel[ 'id' ];
$name = $rel[ 'name' ];
$permalink = $rel[ 'permalink' ];
$photos=$rel['gallery'];
if ( ! empty( $photos ) && is_array($photos) ) {
foreach ( $photos as $photo ) {
echo wp_get_attachment_image($photo['ID'], 'thumbnail');
} //end of foreach
}; //endif ! empty ( $photos )
echo '<a href="'.site_url( trailingslashit( 'products' ) . $rel[ 'permalink' ] ).'"><h4>' .$name.'</h4></a>';
} //end of foreach
} //endif ! empty ( $related )
?>
答案 0 :(得分:0)
如果有人需要类似的东西,这就是我最终的目标:
<?php
$related = $obj->field( 'related_products' );
$photos = $obj->field('related_products.gallery' );
if ( ! empty( $related ) && is_array($related) ) {
foreach ( $related as $rel ) {
$id = $rel[ 'id' ];
$name = $rel[ 'name' ];
$permalink = $rel[ 'permalink' ];
if ( ! empty( $photos ) && is_array($photos) ) {
foreach ( $photos as $photo ) {
echo wp_get_attachment_image($photo['ID'], 'thumbnail');
break;
} //end of foreach
}; //endif ! empty ( $photos )
echo '<a href="'.site_url( trailingslashit( 'products' ) . $rel[ 'permalink' ] ).'"><h4>' .$name.'</h4></a>';
} //end of foreach
} //endif ! empty ( $related )
?>
我想只从图库中显示一个图像,这就是foreach循环中断的原因。要使所有附加图像删除此行。