Get relative URL for images

How do I get relative URL’s for images?

Might be super simple but I can;t seem to figure the best way to get relative images URLs as opposed to getting absolute URLs with:

<?php echo $image->url() ?>


Managed to get it working with the following:

<?php if($image = $project->images()->sortBy('sort', 'asc')->first()) { ?>

    <img src="<?php echo $image->page()->uri(); echo '/',  $project->file()->filename() ?>" alt="Alt Text"/>
  <?php } else { ?>
    <img src="assets/images/hold-image-960x600.jpg"/>
  <?php } ?>


Why do you need relative URLs? Absolute ones are generally more robust.

I need relatives URLs so that I can implement this

contrary to what I’ve posted, this bit of code

<?php echo $image->page()->uri(); echo '/',  $project->file()->filename() ?>

is actually for a ‘data’ attribute. If I get it working I’ll post the finished site here / in Kirby showcase.

But relative URLs won’t work on subpages of your site as they are always relative to the current page. Don’t absolute URLs work with the plugin? Most plugins support and actually recommend that.

Sorry for delay. It’s a 1 page site and the reason for relative as opposed to absolute is for cleaner URLS. Using the realtive URL would give me:

and absolute would be

I think first looks better.

As it turns out I’ve decided no to bother implementing this for now. You saved me some time. Thanks

Ah, now I get what you wanted to achieve.

But wouldn’t the following look even better?

The project part is already in the URL, which makes the filename unique in this case.
And this syntax can be easily achieved using $page->file('something')->filename() as you wrote.


is even better. I have 2 client projects which I think I’m going to use Kirby for (I’ve been experimenting with it for my own site up until know) as It’s such a flexible, quick and satisfying(!) CMS. Thanks for you help.

Forum / support is also v good.


1 Like