4

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.

1 Answer 1

3

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 %} 
1
  • 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! Commented Oct 12, 2015 at 16:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.