Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

This is a beginner question:

How do I create a link to an entry using entry's id?

I tried several combinations, such as {{ entry.getUrl(my_id) }}, {{ entry.url.id(my_id) }}, etc. but failed to found the correct syntax.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The reason why your example fail is that you are using the entry variable. You are trying to call the function getUrl on the model entry, but the function (or model) doesn't exist yet, and has to be created (fetched).

Example:

You can use craft.entries to fetch the EntryModel, and than output its url:

{% set my_id = 5 %}
{% set entry = craft.entries.id(my_id).first() %}
{{ entry.url }}

If you are in an entry loop, it works the same way:

{% for entry in craft.entries.section('blog') %}
    {{ entry.url }}
{% endfor %}
share|improve this answer
    
Thanks! I tried to add this link but all I got is a PHP error: CException — Craft\ElementCriteriaModel and its behaviors do not have a method or closure named "url". Same problem after chaining the 'entry' and 'my_id'. Any idea? –  Romain poirier Jul 6 at 12:21
1  
@Romainpoirier Just added .first() to the 2nd line of the first code block. That should fix your error. –  Brandon Kelly Jul 6 at 13:22
    
Thanks Brandon, it works :-) –  Romain poirier Jul 6 at 14:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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