0

我创建了名为“产品”的高级内容类型 POD。POD 具有将图像上传到图库的关系文件字段:字段名称为“图库”。产品在同一个“产品”窗格中相互关联。我想在前端显示相关领域的图像。到目前为止,我没有将以下代码放入 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 )
?>
4

1 回答 1

0

If anyone would need something similar this is what I end up with:

<?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 )
?>

I wanted to have only one image showing up from gallery which is why there is a break in foreach loop. To have all attached images just delete this line.

于 2014-09-18T10:13:03.993 回答