2

I want to display blog entries within the related entry template linked together with 'entries fields':

-- Entrysection Bla

Blog
-- Blog entry 1 (Linked to Entrysection Bla via 'entries fields')
-- Blog entry 2 (no relations)

Now I want to display Blog entry 1 in Entrysection Bla

{% for entry in craft.entries.section('bla') %}
  {% for entries_fields in craft.entries.section('blog'){# related to ??? #} %}
     <a href="{{ entries_fields.url }}">{{ entries_fields.title }}</a>
  {% endfor %}
{% endfor %}

I figured it has to be done with relatedTo but I cant get it done. How do I have to write this?

-- Edit:

Now my code on Entrysection Bla looks like this:

{% set entries_fields = craft.entries.section('blog').relatedTo({
   sourceElement: entry,
   field: 'blogSaisonKategorie'
}) %}

{% for xy in entries_fields %}
   <a href="{{ xy.url }}">{{ xy.title }}</a>
{% endfor %}

That prints out nothing. No error is displayed as well.

2
  • In your edit, where is entry coming from? Are you still doing the for loop around this code? Or are you relying on entry to be populated by Craft in how you're pointing to this template? – Peter Tell Dec 19 '14 at 17:12
  • I changed sourceElement to targetElement now its bringing up all blog entries without filtering (It even shows the inactive blog entries). At least thats something. Yes I'm looping around the code. I guess my error has something to do with targetElement: entry,. It somehow does't listen to the fieldhandle of the entries-fields fiedltype. – KSPR Dec 26 '14 at 14:49
2

You are correct in your assumption! Using relatedTo with the associated field:

{% set entries_fields = craft.entries.section('blog').relatedTo({
    sourceElement: entry,
    field: 'fieldHandle'
}) %}

Note that field can be the field handle of your Entries Field but it can also be a field ID.

2
  • If I enter the fieldHandle of the 'field-entries' field there is still every entry displayed. I guess I have a mess between when to trigger the blog section and when the Bla section. – KSPR Dec 19 '14 at 16:09
  • Are you entering field-entries as your field handle? I'm pretty sure that field handles can't have "hyphens" in them. field- is something that craft prepends. Go to Settings > Fields > Field Type and find the handle of your field. This is what you should enter in the example above – Peter Tell Dec 19 '14 at 16:26
1

I had a huge mess and I started all over.

Thats the code which works:

      {# Gets the bla section #}
        {% for entry in craft.entries.section('bla') %}
          {# Gets the blog section related to bla #}
          {% set relatedBLogs = craft.entries.section('blog').relatedTo(entry) %}
          {% for entries in relatedBLogs %}
             <a href="{{ entries.url }}">{{ entries.title }}</a>
          {% endfor %}
        {% endfor %}

I thought I had to specify the entries fields but Craft determines the relational field without any declaration :O

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.