1

I am still having a problem looping through related posts.

I get zero, not even the HTML. And there are no Craft template errors. There are plenty of entries that were assigned the same market, so there should be something. AND this exact approach worked perfectly before !@$!@#!

<h5>Similar Projects</h5>

{% set relatedMarket = entry.market.first() %}
{% set relatedEntries = craft.entries.section('posts').relatedTo({ targetElement: relatedMarket, field: 'market'}).id('not'~entry.id) %}

{% for relatedEntry in relatedEntries %}
    <a href="{{ relatedEntry.url }}"><p> Entry title goes here. {{ relatedEntry.title  }} </p></a>
{% endfor %}    

I double checked. My entry section handle is posts. The handle for the posts entry type is notifications. The category group handle is markets. The category entry field handle is market. I included the entry type and category group handles just in case I am missing something.

  • Are you getting any HTML on the page or is it totally blank? Are you in devMode? – Aaron Berkowitz Aug 22 '16 at 13:49
  • I am in devMode. The rest of the page is working. The layout is extended. The entry title, image, body, and category all appear. This section, however, generates nothing. I stripped out the reference to the 'owl carousel` and stripped my example to print just the relatedEntry.title and relatedEntry.url. It still is generating nothing. – Hellyar Aug 22 '16 at 14:16
  • {% set relatedMarket = entry.market.first() %} {% set relatedEntries = craft.entries.section('posts').relatedTo( { targetElement: relatedMarket, field: 'market'}).id('not'~entry.id) %} {% for relatedEntry in relatedEntries %} <a href="{{ relatedEntry.url }}"><p> {{ relatedEntry.title }} </p></a> {% endfor %} – Hellyar Aug 22 '16 at 14:16
  • You could try dumping each variable to debug using {{ dump( relatedMarket ) }} or simply see if anything has been found by applying the length filter like so: {{ relatedEntries|length }}. What is the length of relatedEntries? – Luke Pearce Aug 22 '16 at 14:54
  • 1
    Try using {% set relatedMarket = craft.categories.id( entry.market.id ).first() %} – Luke Pearce Aug 22 '16 at 16:32
4

The code above was missing a space after not

.id('not'~entry.id) should have been .id('not '~entry.id)

This one is an easy one to miss and I have done it a million times.

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.