I'm having trouble limiting next/previous links in one channel to articles by the author.

next/previous links are working correctly on the regular blog but when included on the DJ blog I get unknown property errors in the template.

If I remove the merge line below pagination appears to work, but includes DJ posts not by the current Dj.

url looks like https://domain.com/djs/503/firstName-lastName where 503 is the author.id


{# Set parameters for prev/next elements list #}
{% set params = craft.entries.section(channel).orderBy('title asc') %}

{# if djs channel limit to author based on url segment 2 which is author id #}
{% if craft.app.request.segment(1) == 'djs' %}
  {# get dj to relate and limit next/prev #}
  {% set author = craft.app.request.getSegment(2) %}
  {# {% set dj = craft.users.id(author).one() %} #}

  {# {% set params = craft.entries.section('djBlog').relatedTo(author).orderBy('title asc') %} #}

  {% set params = params | merge({ relatedTo: author }) %}

{% endif %}

Below code is working on all other templates except the DJ blog where I need to restrict pagination to posts by the current author. Only including here in case it may be relevant though I don't think so.

<div class="entry-pagination grid-container"> {# grid-container #}
  <div class="grid-x grid-padding-x">
    <div class="cell small-12 {% if backToPath == 'the-word-for-you-today' %}medium-12{% else %}medium-8 medium-offset-2{% endif %} next-prev">

      {% if prevEntry %}        
        <a href="{{ prevEntry.url }}" class="prev-blog">
          {#<img src="/assets/images/previous.png" alt="">#}
          {{ macros.icon("next", "icon-blue", "40", "40") }}
           Previous
        </a>
      {% else %}
        <a href="" class="prev-blog" disabled="true">
          {#<img src="/assets/images/previous.png" alt="">#}
          {{ macros.icon("next", "icon-blue", "40", "40") }}
           Previous
        </a>    
      {% endif %}

      {% if backTo == 'true' %}
        <a href="/{{ backToPath }}" class="button">
          Back to {{ backToTitle }}
          {# <img src="/assets/images/previous.png" alt=""> #}
        </a><!-- /.button -->
      {% endif %}


      {% if nextEntry %}
        <a href="{{ nextEntry.url }}" class="next-blog"><span>Next</span>{{ macros.icon("next", "icon-blue", "40", "40") }}</a>
      {% else %}
        <a href="" class="next-blog" disabled="true"><span>Next</span>{{ macros.icon("next", "icon-blue", "40", "40") }}</a>
      {% endif %}
    </div><!-- /.cell small-12 -->
  </div><!-- /.grid-x grid-padding-x -->
</div><!-- /.grid-container -->
up vote 1 down vote accepted

You are trying to merge an object of type ElementQuery with an array so your object will be transformed as an array and merged with the new array. Of course you'll end up having an array instead of a query so you can't execute it in order to fetch the entries.

Instead of merging it, why not setting the parameter directly?

{% do param.authorId(author) %}

Furthermore authors are not related to entries (except you set them in an additional field)

  • did not know about {% do blah %} thank you. That worked. – CreateSean Oct 10 at 20:28

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.