Hello everyone,
i tried to output the URL of a cover image today.Unfortunately not the complete URL is given.
My PHP script in the template:
<img src="<?= $article->cover()->url() ?>" />
And the generated HTML:
<img src="- bild.jpg" alt="">
It always adds "- " and the rest of the URL is missing.
Am I doing something wrong here?
Greetings
Tobias
<?php if ( $cover = $article->cover()->toFile() ) : ?>
<img src="<?= $cover->url() ?>" />
<?php endif; ?>
You have to convert the field value to a file object first, that’s what the toFile()
method does. Additionally, it is necessary to check if what is stored in the field actually returns a file object when converted to file.
Thanks a lot! This helped me a lot and loosened the knot in my head.
Your quick answer is one of the reasons I love Kirby.