Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I'm trying to show related entries to current entry by tag ID (which is working fine) but getting hung up when trying to exclude the current entry from the loop. I think I'm having an issue with paginate tags requiring an element criteria model but I'm not able to figure out how to get around this.

{% set segment = craft.request.lastSegment %}
{% for entries in craft.entries.slug(segment) %}
  {% set current = craft.entries.slug(segment) %}
  {% set tagId = entry.myTagFieldHandle.first() %}
  {% set suite = craft.entries.relatedTo(tagId) %}

    {% paginate suite|without(current) as entries %}
      {% for entry in entries %}
        {{ entry.title }}
      {% endfor %}
    {% endpaginate %}

{% endfor %}

Excluding the without filter on paginate outputs everything fine, but includes the current entry. I know there are probably better ways of doing this and I appreciate any help, thanks!

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You're wanting to show a paginated list of related entries from a single entry page based on a tag relationship, correct?

This should point you in the right direction:

{% set segment = craft.request.lastSegment %}

{% for entry in craft.entries.slug(segment) %}

    {% set tagId = entry.myTagFieldHandle.first() %}

    {% set relatedEntries = craft.entries.relatedTo(tagId).id('not ' ~ entry.id) %}

    {% paginate relatedEntries as related %}

        {% for entry in related %}

            {{ entry.title }}

        {% endfor %}

    {% endpaginate %}

{% endfor %}
share|improve this answer
    
That did the trick! Now off to learn what ('not ' ~ entry.id) is all about... –  Steve Jul 18 at 20:32
1  
The tilde in Twig converts all operands into strings and concatenates them thus excluding said entry id from the relationship. twig.sensiolabs.org/doc/templates.html#other-operators –  Ryan Shrum Jul 18 at 20:37
    
Thanks for the explanation Ryan, I definitely have a lot to learn both regarding Craft and Twig. Every comment and solution is a huge help. –  Steve Jul 18 at 20:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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