Image path for cached image without sorting number in url

Hi,

My existing code:

$item->images()->sortBy('sort')->first()->url()

And it gives me this url:

domain.com/content/2-group-page/88-page-name/filename-6.png

And my issue is that when I edit the page the numbers changes, and that leads to broken links externally.

1. Is there a way to get the full url without the sorting number?

2. And is there a way to get the image from the cache with full path also without sorting?

I think I did this at some point but I do not remember and can not find it any where. :confused:

Thank you for all help here in the forum. :slight_smile:

You can create a custom file methods that that creates the url without the numbers.

1 Like

Great. :slight_smile: And you are quick as always @texnixe. But what is unclear to me is where does the code go?

In a plugin file, e.g. /site/plugins/methods.php. Of course, you would have to replace the $file->url() call with a call to your custom method.

1 Like

There issen’t any other way that are easier? The issue happens in the feed plugin that I have modified to fit some custom needs. Is a way to get around this retrieving the cached image or make a thumb?

Here is my code: (/site/plugins/feed/template.php)

<!-- generator="<?php echo $generator ?>" -->
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:atom="http://www.w3.org/2005/Atom">

    <channel>
        <title><?php echo xml($title) ?></title>
        <link><?php echo xml($link) ?></link>
        <generator><?php echo c::get('feed.generator', 'Kirby') ?></generator>
        <lastBuildDate><?php echo date('r', $modified) ?></lastBuildDate>
        <atom:link href="<?php echo xml($url) ?>" rel="self" type="application/rss+xml"/>

        <?php if (!empty($description)): ?>
            <description><?php echo xml($description) ?></description>
        <?php endif ?>

        <?php foreach ($items as $item): ?>
            <item>
                <title><?php echo xml($item->title()) ?></title>
                <link><?php echo xml($item->url()) ?></link>
                <guid><?php echo xml($item->id()) ?></guid>
                <pubDate><?php echo $datefield == 'modified' ? $item->modified('r') : $item->date('r', $datefield) ?></pubDate>
                <description><![CDATA[
                  <? if ($item->hasImages()): ?>
                      <a href="<?php echo xml($item->url()) ?>">
                      <img alt="<?php echo xml($item->title()) ?>"
                      border="0"
                      src="<?php echo xml($item->images()->sortBy('sort')->first()->url()) ?>" />
                      </a>
                  <? endif ?>
                  <?php echo $item->{$textfield}()->kirbytext() ?>
                  <?php if (!empty($showbutton)): ?>
                    <a href="<?php echo xml($item->url()) ?>">
                    <img alt="<?php echo xml($item->title()) ?>"
                    border="0"
                    src="<?php echo xml(url($button)) ?>" />
                    </a>
                  <?php endif ?>
                  ]]></description>
            </item>
        <?php endforeach ?>

    </channel>
</rss>

Do you have any quick and easy solution for the one issue. Is it possible to construct the URL somehow the hacky way @texnixe?

Yes, you can create is like this: