0

How would you go about pulling a selected entry from one channel into another entry using Craft variables?

For example, I have a page with an author channel field that allows a user to select the author from entries in an author channel. Once selected, I am wanting the page to display the author content from the selected Author entry.

I have tried using the following but have gotten no output.

{% if entry.authorTeamMember|length %} 
    {% set myEntry = entry.authorTeamMember.first() %}

    {{entry.authorName}}
    {{entry.authorDescription}}
{% else %}
    <p>No Author</p>
{% endif %}
0

You're close!

You select the author correctly with and assign it to the myEntry variable:

{% set myEntry = entry.authorTeamMember.first() %}

Now you just need to output the data from myEntry instead of entry. i.e.

{% if entry.authorTeamMember|length %} 
    {% set myEntry = entry.authorTeamMember.first() %}

    {{ myEntry.authorName }}
    {{ myEntry.authorDescription }}
{% else %}
    <p>No Author</p>
{% endif %}
| improve this answer | |
  • Thanks! I knew I was overlooking something simple somewhere! – Matthew Dillon Sep 21 '17 at 23:52
  • So working with it, it will pull the first entry in the authorTeamMember channel perfectly but wont recognize other authors if the code is in a loop pulling other entries for other authors. I am thinking it has to do with the .first() tag when I am setting the variable myEntry. Is there a way in the set tag to set entry.authorTeamMember to whatever option a is selected per article? – Matthew Dillon Sep 22 '17 at 2:05
0

So I had to adjust Brads code slightly and tweak it to get what I wanted.

Issue: I was getting some entrys but not all the entrys I wanted when I was pulling a list as one of my entryfields for my page was a channel.

When I was pulling the information for my authorName from myEntry, myEntry was assigned to the channel field and would only pick up and register the first author entry i created. I had to change {{ myEntry.AuthorName }} to {{ myEntry.Title }} as the channel entry for each author had a title field.

{% if entry.authorTeamMember|length %} 
    {% set myEntry = entry.authorTeamMember.first() %}

    {{ myEntry.Title }}
    {{ myEntry.authorDescription }}
{% else %}
    <p>No Author</p>
{% endif %}
| improve this answer | |

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.