texnixe
December 13, 2016, 12:42pm
21
One thing you can do is require the field, of course.
Otherwise:
<?php
if($page->hero()->isNotEmpty()):
$imagelist = $page->hero()->split();
$number = array_rand($imagelist);
$hero = $imagelist[$number];
$image = $page->image($hero);
else:
$image = $page->images()->first();
endif;
?>
<div class="hero" style="background-image: url(<?php echo $image? $image->url(): '' ?>);"></div>
Or if you think the user does not even upload an image, you can even get an image from the assets folder as a last resort.
Hi Sonja,
Starting from this code, is’t possible to insert also the Thumb function to resize by width or/and height, for example, the image in case it was too big?
Normally I use:
echo thumb($image, array('width' => 300, 'height' => 200));
Thanks.
Sure, you might as well use the thumb function.
I get some errors, can you show me how please?
Thank you.
“I get some errors” is not really very meaningful What sort of errors do you get?
<?php
if($page->hero()->isNotEmpty()):
$imagelist = $page->hero()->split();
$number = array_rand($imagelist);
$hero = $imagelist[$number];
$image = $page->image($hero);
else:
$image = $page->images()->first();
endif;
?>
<div class="hero" style="background-image: url(<?php echo $image? thumb($image, array('width' => 300, 'height' => 200))->url(): '' ?>);"></div>
My mistake, it works well, thank you Sonja