2

So, I am new. I want to display the 1-n most recent posts in the same category as the current entry (preferably excluding the current entry) -- i.e., I want to have a section with Other Similar Posts at the bottom of an entry page.

I only have one Category Group = beerStyles. There are multiple categories to choose from in beerStyles. Each news entry includes a field called "style" to assign a category from beerStyles. Each entry can have only 1 category. (Note there is no entries field in news entry, just the style field to select a category form beerStyles.)

<aside>
        <h4 class="text-center">Related Posts</h4>
        <div class="columns">
            <ul class="medium-block-grid-3">  
                {% set category = craft.categories.slug('beerStyles') %}
                {% set entries = craft.entries.relatedTo(category) %}
                {% for entry in entries %}
                <li class="text-center"><a href="{{entry.url}}">{{entry.title}}</a></li> 
                {% endfor %}
            </ul>
        </div>
    </aside>

No error is thrown, but other news entries with the same category assigned are not appearing. Nothing is appearing.

Any thoughts? And thank you in advance for your time and assistance.

P.S. I know I left off the .limit(x) to limit the number of posts I just want to get it working first.

  • I think you forgot to add the category group. For example I've just used: {% set cats = craft.categories.group('magazineCategory') %} – Tibor Szasz Aug 14 '16 at 1:35
  • I did try switching slug to categories. Everything appeared. The output was not filtered (on current entry's category.) – Hellyar Aug 14 '16 at 1:51
  • This should be the correct: craft.categories.group('yourGroup').slug('beerStyles') – Tibor Szasz Aug 14 '16 at 2:34
3

Based on your description, this should do it:

{% if entry.style|length %}
<aside>
    <h4 class="text-center">Related Posts</h4>
    <div class="columns">
        <ul class="medium-block-grid-3">  
            {% set styleCategory = entry.style.first() %}
            {% set relatedEntries = craft.entries.section('news').relatedTo({
                targetElement: styleCategory,
                field: 'style'
            }).id('not '~entry.id) %}
            {% for relatedEntry in relatedEntries %}
                <li class="text-center"><a href="{{relatedEntry.url}}">{{relatedEntry.title}}</a></li> 
            {% endfor %}
        </ul>
    </div>
</aside>
{% endif %}

A few notes:

– I've wrapped the whole "Related posts" in a conditional testing if the entry.style categories field is not empty (if entry.style|length).

– Personal preference, but for readability's sake I find its usually a good idea to be specific/semantic with variable names (e.g. styleCategory instead of just category, relatedEntries instead of entries etc).

– The targetElement and field attributes for the relatedTo() parameter are not required – you could just do .relatedTo(styleCategory). Being specific is a minor performance win and avoids wrong results if you have multiple relations involving the same elements.

– The id('not '~entry.id) prevents the current entry from being returned

  • Thank you for the logic flow. Now it works and I understand why. – Hellyar Aug 14 '16 at 14:36

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.