5

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.

2 Answers 2

11

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.

{% 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>
3
  • 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
    Jun 13, 2014 at 9:48
  • 1
    Macros actually get their own variable scope, so anything defined in them will never conflict with other variables. Jun 13, 2014 at 13:38
  • 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
    Jun 13, 2014 at 14:06
4

Sounds like you just need this:

{% set entry = craft.entries.id(412).first() %}
<a href="{{ entry.url }}">{{ entry.title }}</a>
3
  • 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
    Jun 13, 2014 at 9:50
  • Yep, you're right. Have updated my answer. Jun 13, 2014 at 13:05
  • @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
    Jun 13, 2014 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.