1

I have created a test category called test inside the what-we-do category group. There are three files at play

The first has a line of code that generates the query string on button click

{% for cat in categories %}
    <li class="vr"><a href="{{ entry.url }}?category={{ cat | kebab }}" class="btn btn_plain mix-btn_noWidth">{{ cat.title }}</a></li>
{% endfor %}

The second takes the query string formats it and passes it to the list template. The code looks like

{% if craft.request.queryStringWithoutPath %}
    {% set categoryQueryString = craft.request.QueryStringWithoutPath| replace({"category=" : ""}) %} 
{% else %}
    {% set categoryQueryString = "" %}        
{% endif %}

<ul>
    {% include '_includes/snippets/blog/list.html.twig' with {category : categoryQueryString} %}
</ul>

Then the list code looks like

{% set sections = 'blogPosts' %}
{% paginate craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default)).limit(limit) as pageInfo, pageEntries %}

{% for entry in pageEntries %}
    {% if entry['listingPageText']|default %}
        {% set content = entry.listingPageText %}
    {% endif %}
    <div>
        <p>{{ content }} </p>
    </div>
{% end for %}

This section seems to be working when I dump it but I am not seeing any of my entries rendered

{% paginate craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default)).limit(limit) as pageInfo, pageEntries %}

I'm wondering if there is something simple i'm missing

If I use

{% paginate craft.entries.section(sections).relatedTo(category|default).limit(limit) as pageInfo,pageEntries %}

I can get the posts to appear but it does not filter by category it just defaults and renders all the blog posts.

I'm guessing that's because I have to pass the full category url what-we-do/test assuming the category name is test.

What I really need is it to show the blogposts that are of category test inside the category group what-we-do

1

craft.categories will return you an object of type ElementCriteriaModel

craft.categories.slug('what-we-do/' ~ category|default) sets an attribute of this object, but it does not fetch the elements yet.

The relatedTo parameter needs to be an element, a list of elements, an array and some other staff but it can't handle a query (which the ElementCriteriaModel is)

You have to fetch the element(s) before you pass it to the function with first(), ids(), last() or find()

craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default).first())

Btw: just as an additional note {{ cat | kebab }} will print the title in kebab case. Mostly that will be the slug but if the user changes the title or the slug or inserts something completely different from the beginning, your logic will break

| improve this answer | |

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.