I am trying to do the most basic thing. On my home page I want to load a cover photo as a background image. If I just use use $page->images()->first()
it outputs a full absolute url and for some reason that fails to render, I need to use the absolute relative path /content/1-home/whatever.jpg
. I thought ->url()
is supposed to accomplish that but it prints nothing at all. What gives? I can do ->width()
or ->filename()
or many other things but ->url()
is just blank.
Can you try to add the return $file
to a variable, like this:
<?php
$coverImage = $page->images()->first();
?>
You can check what’s in the variable by using the dump()
helper, to see if this variable contains a Kirby File Object now:
dump($coverImage);
If all is well, you can use this $coverImage
to fetch the url to this file: <?php echo $coverImage->url() ?>
Sorry, nevermind. The truth is, I am using Twig templates and they usually let you do something like {{page.images.first.url}}
. Without using any of the parentheses. But for some reason in the case of url
it did need the parentheses. When I tested with a normal PHP file it worked fine, and then for some reason that was the next thing I checked. It doesn’t return a path relative to the site URL. It returns a full path starting with http://, but that works fine for my purposes.