# Best way to link to a page in a Template

**URL:** <https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 6, 2022, 9:40am UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916 "2022-07-06T09:40:50Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [July 11, 2022, 11:58am UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/21 "2022-07-11T11:58:28Z")

</div>

Trumpets playing!!! Yes. Brilliant.

I’ll try it now

---

<div class="post-metadata">

**Author:** ![rasteiner](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rasteiner/32/4788_2.png) [@rasteiner](https://forum.getkirby.com/u/rasteiner)\
**Post date:** [July 11, 2022, 11:59am UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/22 "2022-07-11T11:59:25Z")

</div>

… Assuming a page with id `newsletter` actually exists in the content root folder.

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [July 11, 2022, 12:01pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/23 "2022-07-11T12:01:34Z")

</div>

Yep. Works as expected.

THANK YOU

---

<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:** [July 11, 2022, 12:05pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/24 "2022-07-11T12:05:36Z")

</div>

> [@rasteiner](#):
>
> `<a href="<?= page('newsletter')->url() ?>">Subscribe to newsletter</a>`

I hate to be a PITA and I know it makes stuff more complicated, and I am repeating myself eternally, but to prevent errors in case the page doesn’t exist or is later renamed, this should be:

```auto
<?php if ($p = page('newsletter'): ?>
  <a href="<?= $p->url() ?>">Subscribe to newsletter</a>
<?php endif ?>

```

@Mark_E In your case, if you have no intention of ever renaming your page, moving it anywhere, or deleting it, you can leave the code as is. Just posting this here for the rest of the world.

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [July 11, 2022, 12:13pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/25 "2022-07-11T12:13:25Z")

</div>

Thanks. No I don’t think I’ll ever rename, move or delete the page – and if I did I’d tweak the link… but I can see that working on a client’s site this would be something to consider.

So the following code/link will still work even if a client moved the page (say made it a child page) or renamed the page? That’s clever!

```auto
<?php if ($p = page('newsletter'): ?>
  <a href="<?= $p->url() ?>">Subscribe to newsletter</a>
<?php endif ?>

```

From my own experiments I believe if the page doesn’t exist, or has been deleted, the link is not displayed.

---

<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:** [July 11, 2022, 12:14pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/26 "2022-07-11T12:14:24Z")

</div>

> [@Mark\_E](#):
>
> So the following code/link will still work even if a client moved the page (say made it a child page) or renamed the page?

No, but it will prevent that an error is thrown if the page doesn’t exist.

> [@Mark\_E](#):
>
> From my own experiments I believe if the page doesn’t exist, or has been deleted, the link is not displayed.

Yes, the link is not shown.

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [July 11, 2022, 12:15pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/27 "2022-07-11T12:15:24Z")

</div>

> [@Mark\_E](#):
>
> So the following code/link will still work even if a client moved the page (say made it a child page) or renamed the page?

> No, but it will prevent that an error is thrown if the page doesn’t exist.

By not displaying the link at all?

---

<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:** [July 11, 2022, 12:17pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/28 "2022-07-11T12:17:21Z")

</div>

> [@Mark\_E](#):
>
> By not displaying the link at all?

Yes, why would you want a link to something that doesn’t exist? If you still wanted an empty link to show up, you could also do it like this:

```auto
<a href="<?= ($p = page('newsletter')) ? $p->url(): '' ?>">Subscribe to newsletter</a>

```

Or if you are on PHP 8.0+, using the so-called “null-safe operator” ([PHP null-safe and null-coalescing operators - DEV Community](https://dev.to/frknasir/php-null-safe-and-null-coalescing-operators-jg2)):

```auto
<a href="<?= page('newsletter')?->url() ?>">Subscribe to newsletter</a>

```

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [July 11, 2022, 1:07pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/29 "2022-07-11T13:07:03Z")

</div>

Thanks for clarifying what happens if a link is broken.

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [May 29, 2023, 6:45pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/30 "2023-05-29T18:45:45Z")

</div>

I’ve been using the following code, in a PHP snippet, to link to a page.

`<a href="<?= page('ethical-design')->url() ?>">`

This works fine, on parent and child pages.

But I’d like to link to an anchor on the ‘ethical-design’ page. I tried:

`<a href="<?= page('ethical-design#eco')->url() ?>">`

But I now get a PHP error in the browser. Is it possible to link to an anchor link on a page?

---

<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:** [May 29, 2023, 7:04pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/31 "2023-05-29T19:04:48Z")

</div>

> [@Mark\_E](#):
>
> `<a href="<?= page('ethical-design#eco')->url() ?>">`

The page helper expects a page id/uuid as attribute, so you cannot simply add something to this, as it will result in null, and then you cannot call `url()` on null. So you have to use string concatenation and add the hash to the url:

```auto
<a href="<?= page('ethical-design)->url() . '#eco' ?>">

```

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [May 29, 2023, 8:37pm UTC](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916/32 "2023-05-29T20:37:00Z")

</div>

Thanks.

[Previous page](https://forum.getkirby.com/t/best-way-to-link-to-a-page-in-a-template/25916.md?page=1)
