# Structure field with default url

**URL:** <https://forum.getkirby.com/t/structure-field-with-default-url/20196>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 29, 2020, 2:51pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196 "2020-11-29T14:51:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dm1986](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dm1986/32/9354_2.png) [@dm1986](https://forum.getkirby.com/u/dm1986)\
**Post date:** [November 29, 2020, 2:51pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/1 "2020-11-29T14:51:51Z")

</div>

Hello together,

I don’t know if the title of my post is as clear as I mentioned. I hope so.

I have a structure field to create a table of content. It contains:

- Select field with the selection of the page
- A number field to give it a number for the chapter
- url field to link the pages on the website

My problem is. **I want to fetch the url of each page automatically in this url field**.

But I don’t really know how I can make it. hope you can help me.

Greetz  
Markus

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 29, 2020, 3:14pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/2 "2020-11-29T15:14:32Z")

</div>

You don‘t need to store the page url at all, get it from the page id stored in your select field in your template by calling toPage(’,’) on the field.

---

<div class="post-metadata">

**Author:** ![dm1986](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dm1986/32/9354_2.png) [@dm1986](https://forum.getkirby.com/u/dm1986)\
**Post date:** [November 29, 2020, 3:49pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/3 "2020-11-29T15:49:57Z")

</div>

Ok. But I don’t create the pages in this structure field.

The file looks like this:

```auto
    label: Inhaltsverzeichnis
    type: structure
    icon: list-numbers
    fields:
      inhaltsseite:
        label: Inhaltsseite
        type: select
        options: query
        query: site.children.published
      kapitel: fields/kapitel
      url: fields/urlverzeichnis
``
So, is your way then also possible?
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 29, 2020, 4:46pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/4 "2020-11-29T16:46:57Z")

</div>

> [@dm1986](#):
>
> `query: site.children.published`

But you select the pages here, don’t you? And from this information you can create the URL.

---

<div class="post-metadata">

**Author:** ![dm1986](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dm1986/32/9354_2.png) [@dm1986](https://forum.getkirby.com/u/dm1986)\
**Post date:** [November 29, 2020, 5:39pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/5 "2020-11-29T17:39:16Z")

</div>

Yes I select the pages from this.

Could I use something like the solution you have posted [here](https://forum.getkirby.com/t/topages-in-multilevel-structure/13041):

> [@\-\>toPages() in multilevel structure](https://forum.getkirby.com/t/topages-in-multilevel-structure/13041/5):
>
> ```auto
> <?php
> 
> $str = $site->header()->toStructure();
> foreach($str as $item) {
> foreach($item->menu()->toStructure() as $menu) {
> dump($menu->links()->toPages());
> };
> }
> ?>
> 
> ```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 29, 2020, 6:30pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/6 "2020-11-29T18:30:10Z")

</div>

It works like this:

```auto
<?php
// replace structurefieldname with name of your field, it's missing from your blueprint definition above
$items = $page->structurefieldname()->toStructure();
foreach ( $items as $item ) :
  if ( $p = $item->inhaltsseite()->toPage() ):
    // from here on, you have a page object and can get all its props
    echo $page->title();
    echo $page->url();
  endif;
endforeach;
?>

```

---

<div class="post-metadata">

**Author:** ![dm1986](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dm1986/32/9354_2.png) [@dm1986](https://forum.getkirby.com/u/dm1986)\
**Post date:** [November 29, 2020, 9:09pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/7 "2020-11-29T21:09:37Z")

</div>

Thanks for your answer.

I have replace the structurefieldname with the field name which I need.  
But now the browser shows me:

```
ParseError
syntax error, unexpected 'endforeach' (T_ENDFOREACH)

```

It’s not important anymore. This idea is actually something to high for my knowledge of PHP.  
So, I will look after an easier solution.

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 29, 2020, 9:13pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/8 "2020-11-29T21:13:51Z")

</div>

I corrected my code above, I used a curly brace instead of a colon.

---

<div class="post-metadata">

**Author:** ![dm1986](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dm1986/32/9354_2.png) [@dm1986](https://forum.getkirby.com/u/dm1986)\
**Post date:** [November 29, 2020, 9:16pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/9 "2020-11-29T21:16:56Z")

</div>

Thx a lot. Now it works.

---

<div class="post-metadata">

**Author:** ![dm1986](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dm1986/32/9354_2.png) [@dm1986](https://forum.getkirby.com/u/dm1986)\
**Post date:** [November 29, 2020, 10:08pm UTC](https://forum.getkirby.com/t/structure-field-with-default-url/20196/10 "2020-11-29T22:08:34Z")

</div>

I have tested an alternative way.

```
   <?php foreach ($page->inhaltsverzeichnis()->toStructure() as $toc): ?>

  <li>
    <?= $toc->kapitel() ?> <a href="<?= $toc->url() ?>"><?= $toc->inhaltsseite() ?></a>
    <div>
      <?= $toc->kurzText() ?>
    </div>
  </li>

<?php endforeach ?>

```

Now it shows also the url of each link.
