Url gets an added directory in path and I don't know why

Hi everyone, this is probably simple but I read the docs, tried multiple things and went through old forum posts - until now without luck and I don’t quite know how to address the issue. I am hoping that you can point me in a direction to fix my error.

I have a structured field I within my site blueprint with which I select internal pages to be shown in the footer. This is what it looks like:

internalLinks:
    label: Internal Links
    help: Select internal pages to be shown in footer
    type: structure
    fields:
      title:
        label: Page Title
        type: text
      url:
        label: Internal page
        type: pages
        multiple: false
        subpages: true  

Within my footer snippet I have following foreach loop:

 <ul>
    <?php foreach ($site->internalLinks()->toStructure() as $intLink) : ?> 
         <li>
            <a href="<?= $intLink->url()->toPages() ?>">
               <span><?= $intLink->title() ?></span>
            </a>
         </li>
     <?php endforeach ?>  
 </ul>

Basically everything works as expected. I noticed though that when I am viewing an individual note (I am using the starterkit to base my stuff of) “/notes/” is getting added to the url and thus leading to a not found error.

Why is this happening?

You need to use like following while you’re using pages field:

<?= $intLink->url()->toPage()->url() ?>
1 Like

Just as additional information: The toPages() field method you used above returns a pages collection instead of a page object. toPages() is intended for pages fields that use multiple pages. Then you need to loop through this collection.

Thanks for your help @ahmetbora. I didn’t consider that until I saw it. Now it makes absolute sense. And thank you for clarifying @texnixe. There is still so much to learn. I wish I’d be a bit faster… :slight_smile: