1

I'm really struggling with displaying news entry that are only relevant to the users' location.

Here is my structure:

User

Country - > is a dropdown with a single value (lets say UK)

Regions is a category that contains

name -> string (for example Europe)

countries -> a checkbox with a list of countries (UK, FR.. etc)

News which has a Region category

So what I have now is this:

{% for entry in craft.entries.section('news') %}
{# loop through regions  #}  
    {% for region in entry.regions %}
        {# loop region's countries  #}
        {% for targetCountry in region.countries %}
            {% if targetCountry == currentUser.country %} 
                {% set visibility = true %}  
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endfor %}

This works, however, since the checking occurs after I get all the entries I can't use pagination. Is there a nicer way of doing this?

  • Try changing the outer loop to the paginate tag {% paginate craft.entries.section('news').relatedTo(entry).limit('12') as variablename %} which doesn't need a closing tag or this link might help mijingo.com/blog/using-categories-in-craft-cms – mention79 Jan 17 '17 at 15:01
  • @mention79 , sorry don't get it, can you be a bit more specific, please? – Lukigi Jan 17 '17 at 15:07
  • So refering specifically to the pagination point at the end of our post. I meant change the first line of code {% for entry in craft.entries.section('news') %} to something like {% paginate craft.entries.section('news').relatedTo(entry).limit('12') as regions %} the .relatedTo(entry) bit is to reference the categories linked to the entry and .limit('12') would give you 12 entries before it did a new page. And the final bit is the variable name that gets referenced in the for loop below. So regions and then in the one below it would change to {% for region in regions %} – mention79 Jan 17 '17 at 15:14
  • If I replace the first loop with your suggestion, I'm getting an error message because '.relatedTo(entry)' is relating to something that doesn't exist as there is no entry at this point. Am I missing something? – Lukigi Jan 17 '17 at 16:31
  • Drop the .relatedTo(entry) possibly – mention79 Jan 17 '17 at 16:55
1

One nice way to handle things like this is to collect the ids for all the entries you want, use those ids for the ElementCriteriaModel you need for the pagination, and then paginate.

Collect the ids (use merge to create an array of the ids):

{% set ids = [] %}

{% for entry in craft.entries.section('news') %}
  {# loop through regions  #}  
  {% for region in entry.regions %}
    {# loop region's countries  #}
    {% for targetCountry in region.countries %}
      {% if targetCountry == currentUser.country %} 
        {% set ids = ids | merge([entry.id]) %}  
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endfor %}

Get the entries for pagination:

{% set unpaginated = craft.entries({
  id: ids,
  limit: 10
}) %}

Finally, the pagination:

{% paginate unpaginated as pageInfo, pageEntries %}
...

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.