<p>Sounds like you just need this:</p>

<pre><code>{% set entry = craft.entries.id(412).first() %}
&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
</code></pre>

CC BY-SA 3.0
2014-06-13T09:50:01.653Z
i don't think that will work. i believe you need to use craft.entries.id(412) to fetch the entry by id.
Ben Croker
CC BY-SA 3.0
2014-06-13T13:05:42.040Z
Yep, you're right. Have updated my answer.
Clive Portman
CC BY-SA 3.0
2014-06-13T14:14:54.350Z
@Clive, `craft.entries.id(412)` returns an object where you have to pick the first (and only) element to get the entry with that id.
carlcs
CC BY-SA 3.0
2014-06-13T09:34:30.350Z
2014-06-13T17:03:39.117Z
2014-06-13T17:03:39.117Z
Clive Portman
<p>This is all you need:</p>

<pre><code>{% set linkedEntry = craft.entries.id(42).first() %}
&lt;a href="{{ linkedEntry.url }}"&gt;{{ linkedEntry.title }}&lt;/a&gt;
</code></pre>

<p>If you use this in many places you can also make a <a href="http://twig.sensiolabs.org/doc/tags/macro.html">macro</a>.</p>

<pre><code>{% macro linkToEntry(entryId) %} 

    {# Get the entry with the selected ID #}
    {% set linkedEntry = craft.entries.id(entryId).first() %}

    {# Make sure the entry exists and output the link #}
    {% if linkedEntry %}
        &lt;a href="{{ linkedEntry.url }}"&gt;{{ linkedEntry.title }}&lt;/a&gt;
    {% endif %}

{% endmacro %}
</code></pre>

<p>It's nice to have macros in a separate file and import it into your templates. I really love using macros for this kind of code snippets, very useful and DRY.</p>

<pre><code>{% import "macros.html" as macros %}

&lt;li&gt;{{ macros.linkToEntry(42) }}&lt;/li&gt;
&lt;li&gt;{{ macros.linkToEntry(17) }}&lt;/li&gt;
&lt;li&gt;{{ macros.linkToEntry(20) }}&lt;/li&gt;
</code></pre>

CC BY-SA 3.0
2014-06-13T09:48:42.513Z
this is a nice solution but i would recommend changing the variable name "entry" to something else, for example "specialEntry", to avoid conflicting with the currently loaded entry based on the url.
Ben Croker
CC BY-SA 3.0
2014-06-13T13:38:47.653Z
Macros actually get their own variable scope, so anything defined in them will never conflict with other variables.
Brandon Kelly
CC BY-SA 3.0
2014-06-13T14:06:06.043Z
Ahh Macros, now that's a whole new world to me, and frankly stackexchange is perfect to get concepts like this out there. Ultimately a simple one line tag was what I was after to do this and a macro is the way around that. In was concerned doing it this way that too many queries may be run, but I'm probably stuck in the EE mindset there. Can always introduce caching where necessary anyway I guess.
neekster
CC BY-SA 3.0
2014-06-13T09:46:12.403Z
2014-06-13T19:42:13.457Z
2014-06-13T19:42:13.457Z
carlcs
<p>I have a number of places in my templates where a link to a particular entry is needed to be hardcoded into the template, but we still want that links URL and title to be dynamic, so if the URL or Title of the entry is updated, this is reflected.</p>

<p>Is there a nice lightweight simple snipped of code we can use to fetch the URL and Title of an entry when supplied with a valid entry ID? I've checked through the documentation but can't see anything obvious. </p>

<p>Particularly after something lightweight that isn't going to add significant numbers of DB queries to a page load. </p>

<templating><entries>
Tag to fetch URL and Title of a entry when supplied it's ID
# Tag to fetch URL and Title of a entry when supplied it's ID

(Asked by: username_0 on 2014-06-13)

I have a number of places in my templates where a link to a particular entry is needed to be hardcoded into the template, but we still want that links URL and title to be dynamic, so if the URL or Title of the entry is updated, this is reflected.

Is there a nice lightweight simple snipped of code we can use to fetch the URL and Title of an entry when supplied with a valid entry ID? I've checked through the documentation but can't see anything obvious.

Particularly after something lightweight that isn't going to add significant numbers of DB queries to a page load.

**Tags:** templating, entries

---

## 2 answers

### Answer by username_3 on 2014-06-13. Score: 11 | Accepted answer

This is all you need:
{% set linkedEntry = craft.entries.id(42).first() %}
{{ linkedEntry.title }}

If you use this in many places you can also make a macro.
{% macro linkToEntry(entryId) %} 

    {# Get the entry with the selected ID #}
    {% set linkedEntry = craft.entries.id(entryId).first() %}

    {# Make sure the entry exists and output the link #}
    {% if linkedEntry %}
        {{ linkedEntry.title }}
    {% endif %}

{% endmacro %}

It's nice to have macros in a separate file and import it into your templates. I really love using macros for this kind of code snippets, very useful and DRY.
{% import "macros.html" as macros %}

{{ macros.linkToEntry(42) }}
{{ macros.linkToEntry(17) }}
{{ macros.linkToEntry(20) }}

> **Comments:**
> **username_2**: this is a nice solution but i would recommend changing the variable name "entry" to something else, for example "specialEntry", to avoid conflicting with the currently loaded entry based on the url.  
> **username_4**: Macros actually get their own variable scope, so anything defined in them will never conflict with other variables.  
> **username_0**: Ahh Macros, now that's a whole new world to me, and frankly stackexchange is perfect to get concepts like this out there. Ultimately a simple one line tag was what I was after to do this and a macro is the way around that. In was concerned doing it this way that too many queries may be run, but I'm probably stuck in the EE mindset there. Can always introduce caching where necessary anyway I guess.  

---

### Answer by username_1 on 2014-06-13. Score: 4

Sounds like you just need this:
{% set entry = craft.entries.id(412).first() %}
{{ entry.title }}

> **Comments:**
> **username_2**: i don't think that will work. i believe you need to use craft.entries.id(412) to fetch the entry by id.  
> **username_1**: Yep, you're right. Have updated my answer.  
> **username_3**: @Clive, `craft.entries.id(412)` returns an object where you have to pick the first (and only) element to get the entry with that id.  

---

