0

I have created Advanced Content Type POD called 'product'. POD has relationship file field to upload images to a gallery: field name is "gallery". Products are related to each other within the same 'product' pod. I would like to show an image from relevant field on a front-end. So far i had no luck by putting following code into PODS template:

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

1 Answers1

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.

  • Instead of doing a for each loop to get the first item in the array, You should be able to just use $photos[0] – JPollock Sep 18 '14 at 17:36