mxb
April 16, 2015, 1:38pm
1
Hi
Is there anyway to include a div element only if there is actually any images/files
in the child/post. In my current code it will add the div element to every child/post even those that does not include images/files.
<?php foreach ($exhibition->images() as $image): ?>
<div class="exhibit">
<img src="<?php echo $image->url(); ?>">
</div>
<?php endforeach ?>
Very thankful for helps.
Xx
<?php if($images = $exhibition->images()) : ?>
<?php foreach ($images as $image): ?>
<div class="exhibit">
<img src="<?php echo $image->url(); ?>">
</div>
<?php endforeach ?>
<?php endif ?>
mxb
April 16, 2015, 2:29pm
3
Thanks texnixe! it works like a charm only the image gets the div tag.
however each image within the post gets the tag is there away to encapsulate all the images instead of each of them
<div class="exhibit">
<img src="">
</div>
<div class="exhibit">
<img src="">
</div>
to
<div class="exhibit">
<img src="">
<img src="">
</div>
<?php if($images = $exhibition->images()) : ?>
<div class="exhibit">
<?php foreach ($images as $image): ?>
<img src="<?php echo $image->url(); ?>">
<?php endforeach ?>
</div>
<?php endif ?>
mxb
April 16, 2015, 2:45pm
5
I’m really sorry for these questions texnixe, really appreciate the help.
The code still having some issues with the divs being added when there is no images in the post.
<?php foreach(page('exhibitons')->children() as $exhibition): ?>
<?php if($images = $exhibition->images()) : ?>
<div class="exhibit">
<?php foreach ($images as $image): ?>
<img src="">
<?php endforeach ?>
</div>
<?php endif ?>
<?php endforeach ?>
What exactly do you want to achieve? Do you want a container for every child page or one container for all of the images?
mxb
April 16, 2015, 4:41pm
7
one container for all of the images/files for each child (exhibitions) on the (exhibition) page
I’m hoping to be able to put all / images per post into a slideshow and keep it to plain text when there is no images.
Sorry, guess I made a mistake, this code should work and create a div for each child page of exhibitions if that child has images:
<?php foreach(page('exhibitions')->children() as $child): ?>
<?php if($child->hasImages()) : ?>
<div class="exhibit">
<?php foreach ($child->images() as $image): ?>
<img src="<?php echo $image->url(); ?>">
<?php endforeach ?>
</div>
<?php endif ?>
<?php endforeach ?>
mxb
April 16, 2015, 5:23pm
9
woohoo! it works appreciate all the help. hasImages(); did the magic