4

I have a bunch of entries in a section called 'artikkel'. The 14 first of these entries are listed out in another section called 'forside'. Now I need to list out the entries that's not already been shown. Printing out the first 14 entries looks like this:

{% for entry in entry.artikkelbolk1.limit(14) %}
    <div class="older_articles">
        <!--print out article content-->
    </div>
{% endfor %}

The rest of the articles that I'm loading in with ajax is like this:

{% for entry in craft.entries.section('artikkel').order('dateCreated desc') %}
    <div class="older_articles ajax_article">
        <!--print out article content-->
    </div>
{% endfor %}

Of course the last one now prints all the articles that's already been printed before. PS: This is the first time I'm working with Craft.

3

This is how you're getting your "Forside" articles:

{% for entry in entry.artikkelbolk1.limit(14) %}
    <!--print out article content-->
{% endfor %}

this let me assume you're using an entries field to "collect" articles from your "artikkelbolk1" section. The Twig syntax you're using is actually a shortcut for this:

{% for entry in craft.entries({
    relatedTo: { sourceElement: entry, field: 'artikkelbolk1' },
    order:     'sortOrder',
    limit:     14
}) %}
    <!--print out article content-->
{% endfor %}

So it get's you all entries related to your current entry, the preset entry model of your "forside" section.

To get all articles not listed on "Forside" you'd do this. First, grab those "forside" entries again, but you're only interested in the IDs of the entry models, that's why you now append the ids() method. Then join the IDs into a string with the following syntax (use Twig's join filter for that!):

'and, not 42, not 82, not 20'

So it basically lists all entry IDs prefixed with 'not'; the 'and' at the beginning just makes Craft aware that you're listing multiple values. With this string you can now go ahead and exclude your entries from an ElementCriteriaModel. To do that simply pass the string into the id parameter.

{% set forside = craft.entries.section('forside').first() %}

{% set forsideEntriesIds = craft.entries({
    relatedTo: { sourceElement: forside, field: 'artikkelbolk1' },
    order:     'dateCreated desc',
    limit:     14
}).ids() %}

{% set idParam = 'and, not ' ~ forsideEntriesIds|join(', not ') %}

{% for entry in craft.entries.section('artikkel').id(idParam).order('dateCreated desc') %}
    <!--print out article content-->
{% endfor %}
| improve this answer | |
  • Hey, thanks so much for the answer. Unfortunately it doesn't work and I'm going crazy. I'll try to explain one more time. I'm printing out my articles in index.html like this: {% for entry in entry.artikkelbolk1.limit(6).offset(1) %} {# content #} {% endfor %} {% for entry in entry.artikkelbolk2.limit(6).offset(1) %} {# content #} {% endfor %} Now I'm listing all the articles in loadmore.html: {% for entry in craft.entries.section('artikkel').order('dateCreated desc') %} {# content #} {% endfor %} I just can't find a way to print out all the ones that's not been listed – steve1938 Apr 27 '15 at 8:38
  • This works in my code when replacing the existing code: {% for entry in craft.entries({ relatedTo: { sourceElement: entry, field: 'artikkelbolk1' }, order: 'sortOrder', limit: 14 }) %} , but you lost me on grabbing the id's and the code under does not work :( – steve1938 Apr 27 '15 at 8:45
  • It might be that the code would work if i just had one 'artikkelbolk', but I have 'artikkelbolk1' and 'artikkelbolk2' with both have seven articles in them that makes 14 articles. the code would be something like this?: {% set forside = craft.entries.section('forside').first() %} {% set forsideEntriesIds = craft.entries({ relatedTo: { sourceElement: forside, field: 'artikkelbolk1, artikkelbolk2' }, order: 'dateCreated desc', limit: 14 }).ids() %} {% set idParam = 'and, not ' ~ forsideEntriesIds|join(', not') %} – steve1938 Apr 27 '15 at 8:51

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.