<p>You could write a <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow">Twig macro</a> to get the localized URL from an entry ID. Import it from _layout.html for example.</p>

<pre><code>{% macro urlFromId(entryId) %}
    {%- set entry = craft.entries.id(entryId).first() %}
    {{- entry ? entry.url -}}
{% endmacro %}

{% import _self as macros %}
</code></pre>

<p>Then use this more concise code everywhere in your templates.</p>

<pre><code>&lt;a class="button" href="{{ macros.urlFromId(14) }}"&gt;{{ "Neem contact op"|t }}&lt;/a&gt;
</code></pre>

CC BY-SA 3.0
2016-03-08T13:25:36.760Z
2016-03-08T13:25:36.760Z
carlcs
<p>Currently i am using this approach for linking to a entry(single page) of a structure, for example a cta button on the homepage which links to the contact page.</p>

<pre><code>{% cache %}
    {% set single = craft.entries.id('14').first() %}
    {% if single %}
        &lt;a class="button button--full t-spacing" href="{{ single.url }}"&gt;{{ "Neem contact op" | translate }}&lt;/a&gt;
    {% endif %}
{% endcache %}
</code></pre>

<p>Or for example a "read more" button next to some results of some structure without a index. Because thats set in the structure "pages" as a entry type.</p>

<p>But i feel like this is somewhat hacky..
Of course you can link to a page like so:</p>

<pre><code>{{ url('contact') }}"
</code></pre>

<p>or</p>

<pre><code>href="{{ "contact"|translate }}
</code></pre>

<p>But url() doesn't translate and what if the slug is changed by a user of the CP.
So what is the usual way to get around this, are there any best practises?
So far i can only think of linking all the individual buttons/links to entry fields in the CP, which the user has to fill in.</p>

<localization><entry><structure><multi-environment>
Linking a page in a multilanguage website
# Linking a page in a multilanguage website

(Asked by: username_0 on 2016-03-08)

Currently i am using this approach for linking to a entry(single page) of a structure, for example a cta button on the homepage which links to the contact page.
{% cache %}
    {% set single = craft.entries.id('14').first() %}
    {% if single %}
        {{ "Neem contact op" | translate }}
    {% endif %}
{% endcache %}

Or for example a "read more" button next to some results of some structure without a index. Because thats set in the structure "pages" as a entry type.
But i feel like this is somewhat hacky..
Of course you can link to a page like so:
{{ url('contact') }}"

or
href="{{ "contact"|translate }}

But url() doesn't translate and what if the slug is changed by a user of the CP.
So what is the usual way to get around this, are there any best practises?
So far i can only think of linking all the individual buttons/links to entry fields in the CP, which the user has to fill in.

**Tags:** localization, entry, structure, multi-environment


---

## 1 answers

### Answer by username_1 on 2016-03-08. Score: 4

You could write a Twig macro to get the localized URL from an entry ID. Import it from _layout.html for example.
{% macro urlFromId(entryId) %}
    {%- set entry = craft.entries.id(entryId).first() %}
    {{- entry ? entry.url -}}
{% endmacro %}

{% import _self as macros %}

Then use this more concise code everywhere in your templates.
{{ "Neem contact op"|t }}


---

