Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T09:50:01.653Z
Answers.Text: i don't think that will work. i believe you need to use craft.entries.id(412) to fetch the entry by id.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T13:05:42.040Z
Answers.Text: Yep, you're right. Have updated my answer.
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T14:14:54.350Z
Answers.Text: @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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T09:34:30.350Z
Answers.LastActivityDate: 2014-06-13T17:03:39.117Z
Answers.LastEditDate: 2014-06-13T17:03:39.117Z
Answers.UserDisplayName: Clive Portman
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T09:48:42.513Z
Answers.Text: 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.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T13:38:47.653Z
Answers.Text: Macros actually get their own variable scope, so anything defined in them will never conflict with other variables.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T14:06:06.043Z
Answers.Text: 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.
Answers.UserDisplayName: neekster
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T09:46:12.403Z
Answers.LastActivityDate: 2014-06-13T19:42:13.457Z
Answers.LastEditDate: 2014-06-13T19:42:13.457Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <templating><entries>
Title: Tag to fetch URL and Title of a entry when supplied it's ID
ThreadText: # 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() %}
  <a href="{{ linkedEntry.url }}">{{ linkedEntry.title }}</a>
  
  ```
  
  If you use this in many places you can also make a [macro](http://twig.sensiolabs.org/doc/tags/macro.html).
  
  ```
  {% 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 %}
          <a href="{{ linkedEntry.url }}">{{ linkedEntry.title }}</a>
      {% 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 %}
  
  <li>{{ macros.linkToEntry(42) }}</li>
  <li>{{ macros.linkToEntry(17) }}</li>
  <li>{{ macros.linkToEntry(20) }}</li>
  
  ```
  
  > **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() %}
  <a href="{{ entry.url }}">{{ entry.title }}</a>
  
  ```
  
  > **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**: @username_1, `craft.entries.id(412)` returns an object where you have to pick the first (and only) element to get the entry with that id.  
  
  ---
  
  