Related Content
Created 6 years ago by psaunders

You can get a link to posts that have the same category with:

 <a href="{{post.category.route('view')}}" class="posts__category">
    {{ post.category.name }}
</a>

You can get a kind of useless list of tags (without links) with:

{{ decorated.labels('success')|join(' ')|raw }}

TWIG really doesn't work for me, but after a while I figured out that this gets you a list of linked tags:

{% for tag in post.tags.value %}
    <a href="/posts/tags/{{ tag }}">{{ tag }}</a>
{% endfor %}

But what I really want is a list of related posts that use the inbuilt tags. Something like $post->related_by_tag which has one occurrence of any other tagged things.

piterden  —  6 years ago

You can extend TagsFieldTypePresenter and move your loop there. Look how it is done for labels() method.

ryanthompson  —  6 years ago

This can certainly be done but would be on a tag by tag basis. Twig is a wrapper for PHP so using the entries function (query builder) we can do something like this:

{% set related = entries('posts').where('id', '!=', post.id).whereLike('tags', post.tags.value|first).get() %}

For example to get posts containing the same tag as the first of the current post. Assuming post is available where you're at in the templates. Could always access the current post like template.post as well.

Hopefully this makes sense!

If you want to add your own custom logic for some better relational building you can add a hook and call it in twig too: https://pyrocms.com/documentation/streams-platform/1.3/services/hooks

Then call it like:

{% set related = posts.myCustomRelatedHook() %}

Hope this helps!

psaunders  —  6 years ago

@piterden Not sure how you're supposed to tell the CMS to override a presenter, I'm guessing it'd be something like detecting a file with the same name in the users addons/ folder?

Regardless, this seems like a core function of a tagging system so I created a PR to add related entries to the field:
https://github.com/anomalylabs/tags-field_type/pull/12

piterden  —  6 years ago