Mara Barn Mara Barn - 4 years ago 68
PHP Question

Need output to display all images

I have this code where I'm trying to display all three images attached to visual composer's custom "attach_images" element type that has param "macimgs". The problem is that only the last one image will display and if I inspect the container that holds images, I see only one image inside instead of three.

Any ideas of what to modify here?

$gallery = shortcode_atts(
array(
'macimgs' => 'macimgs',
), $atts );

$image_ids=explode(',',$gallery['macimgs']);

$image_no = 1;
foreach( $image_ids as $image_id ){
$images = wp_get_attachment_image_src( $image_id, 'full' );
$output ='
<img src="'. $images[0] .'" alt="" />
';
$image_no++;
}
return $output;

}

Answer Source

i think you forget to concatenate the $output variable. try this code

$gallery = shortcode_atts(
    array(
        'macimgs'      =>  'macimgs',
    ), $atts );

$image_ids = explode(',',$gallery['macimgs']);
$output = '';    
$image_no = 1;

foreach( $image_ids as $image_id ){
    $images = wp_get_attachment_image_src( $image_id, 'full' );
    $output .='<img src="'. $images[0] .'" alt="" />';
    $image_no++;
}
return $output;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download