Skip to main content
re-added "redundant" brackets after first() because this is how it is written in the docs
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140

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>

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>

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>
removed redundant brackets after "first"
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140

This is all you need:

{# Get the entry with the selected ID #}
{% 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>

This is all you need:

{# Get the entry with the selected ID #}
{% 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>

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>

This is all you need:

{# Get the entry with the selected ID #}
{% 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>

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>

This is all you need:

{# Get the entry with the selected ID #}
{% 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>
Linked "macro" to http://twig.sensiolabs.org/doc/tags/macro.html
Source Link
Brandon Kelly
  • 34.4k
  • 2
  • 74
  • 139
Loading
corrected code errors and added comments
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
changed code to call that macro multiple times, to illustrate the value
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
typo
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
added 115 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading