Dan
January 10, 2017, 12:48pm
1
Hi!
I know how to fetch a specific image
<?php if($image = $page->image('myimage.jpg')): ?>
but how to fetch every image containing “movie-” in the title (for example: movie-one.jpg) and only if they are available?
<?php if($image = $page->images()->filterBy('filename', '*=', 'movie-')): ?>
<div class="row small-up-2 medium-up-3 large-up-1">
<?php foreach($page->images()->filterBy('filename', '*=', 'movie-') as $image): ?>
<div class="column column-block">
<img src="<?php echo $image->url() ?>" alt="<?php echo $image->title() ?>">
</div>
<?php endforeach ?>
</div>
<?php endif ?>
The first line in my code don’t work.
Thanks for any help
Dan
Edit: I want to show the code block "div class=“row … /div” only if the images are available!
Filtering all the images of a page will guarantee that the image is available. So this should be enough:
<div class="row small-up-2 medium-up-3 large-up-1">
<?php foreach($page->images()->filterBy('filename', '*=', 'movie-') as $image): ?>
<div class="column column-block">
<img src="<?php echo $image->url() ?>" alt="<?php echo $image->title() ?>">
</div>
<?php endforeach ?>
</div>
```
Dan
January 10, 2017, 12:55pm
3
Yes sorry thats why I add:
I want to show the code block "div class=“row … /div” only if the images are available!
If I understood you correctly this would be what you want:
<?php foreach($page->images()->filterBy('filename', '*=', 'movie-') as $image): ?>
<div class="row small-up-2 medium-up-3 large-up-1">
<div class="column column-block">
<img src="<?php echo $image->url() ?>" alt="<?php echo $image->title() ?>">
</div>
</div>
<?php endforeach ?>
Maybe I don’t understand what you mean by “available”. You only really need the if condition if you have an image selector field because the selected image could have been deleted.
EDIT: Okay, I guess texnixe nailed it. “When there are images with this filename”
texnixe
January 10, 2017, 12:57pm
5
<?php
$images = $page->images()->filterBy('filename', '*=', 'movie-');
if($images->count() > 0): ?>
<div class="row small-up-2 medium-up-3 large-up-1">
<?php foreach($images as $image): ?>
<div class="column column-block">
<img src="<?php echo $image->url() ?>" alt="<?php echo $image->title() ?>">
</div>
<?php endforeach ?>
</div>
<?php endif ?>
```
Dan
January 10, 2017, 1:03pm
6
Ahh! YES! Ok thank you texnixe and thguenther! Have a nice day!