Learn

relate

Retrieves content from a relationship.

{{ relate:[field] }}

Relate allows you to fetch content from a relationship using a very simple tag syntax. Relate tags can even be nested inside each other.

It is designed to work hand in hand with the Suggest field.

Sample Usage

Let’s say you have a blog post with a Suggest field called similar_posts that you use to set, you guessed it, similar posts. That relationship data is saved (by default) as a YAML list of URLs, like so:

---
title: Why Bears are Awesome
similar_posts:
  - /blog/my-pet-fox
  - /blog/i-caught-a-fish-with-my-hands
---
Bears. Beets. Battlestar Galactica.

With Relate you can fetch the field data from those entries with a single tag:

<ul class="similar-posts">
  {{ relate:similar_posts }}
      <li><a href="{{ url }}">{{ title }}</a></li>
  {{ /relate:similar_posts }}
</ul>

Previously your templates would have looked something like this:

<ul class="similar-posts">
  {{ entries:listing conditions="{ similar_posts|option_list }" }}
    <li><a href="{{ url }}">{{ title }}</a></li>
  {{ /entries:listing }}
</ul>

Or even this:

<ul class="similar-posts">
  {{ similar_posts }}
    {{ get_content from="{ value }" }}
      <li><a href="{{ url }}">{{ title }}</a></li>
    {{ /get_content }}
  {{ /similar_posts }}
</ul>

Parameters

show_hidden

Boolean setting to show hidden files or not

Default: false

{{ relate:field_name show_hidden="false" }}

show_past

Boolean setting to enable date-based entries that have dates in the past.

Default: true

{{ relate:field_name show_past="true" }}

show_future

Boolean setting to enable date-based entries that have dates in the future.

Default: false

{{ relate:field_name show_future="true" }}

conditions

Conditions let you filter on and and every custom field in your entry data with the use a comma-separated string of key:value pairs.

{{ relate:field_name conditions="author:jack" }}
{{ relate:field_name conditions="type:beef|pork|bacon" }}

You can also string together multiple conditions by separating them with commas:

conditions="author:jack,featured:yes"

You can even perform “not” conditions by adding a ! mark:

conditions="author:!jack"

This article was last updated on September 13th, 2016. Find an error? Please let us know!