3

I thought this would be simple but I am having no luck with it. I have a channel called "Posts" and for each Post there an "entry" custom field which lists all "Projects" (a structure). The user can select up to 3 of these within a Post.

The idea is to relate a current news Post with an existing project. In my Project entry template I basically want to check to see if the current project has been selected in the "relatedProjects" entry field. If it has, I need it to show a "Related Posts" title and then below that list each Post.

The thing I can't fathom out is how to have a section title that doesn't appear in the loop for each Post.

2

You can get the posts related to your "entry" (the currently shown project) with a craft.entries element criteria model where you set the relatedTo parameter like so:

{% set relatedPosts = craft.entries.relatedTo({
    targetElement: entry
}) %}

Then check if this does find anything using Twig's length filter:

{% if relatedPosts|length %}
    <aside>
        {% for relatedPost in relatedPosts %}
            {{ relatedPost.title }}
        {% endfor %}
    </aside>
{% endif %}
| improve this answer | |
  • Thanks so much. I had actually worked out a way of getting the results but it was a much more convoluted way, with nested for loops. This was you don't even need to get the values of the entries field and then compare it to the title of the current entry, which is what I was doing. The above works perfectly! – Anna Oct 12 '15 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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