1

I am trying out Sprig's "Load More" Recipe and I am trying to call up entries that are related to the current category page. But when I add .relatedTo(category) to the entry model, it doesn't work–it still just shows all the entries in the channel. I've used this .relatedTo(category) method elsewhere on the page (not using Sprig) and it works just fine. How can I call up only the entries that match the current category page in this Sprig recipe?

_category.twig

{{ sprig('_layouts/_categories/_loadMore', {limit: 6}) }}
{{ sprig.script }}

_loadMore.twig

{% set offset = offset ?? 6 %}
{% set limit = limit ?? 10 %}

{% set entries = craft.entries.section('aBetterPost').offset(offset).limit(limit).relatedTo(category).all() %}

{% for entry in entries %}

    <a href="{{ entry.url }}" class="more-post">
        <div class="more-post-img-wrap">
            {% set featuredImage = entry.featuredImage.one() %}
            <img src="{{ featuredImage.getUrl }}" class="more-post-img">
            <div class="red-overlay"></div>
        </div>
        <div class="more-post-content">
            <div class="more-post-title">
                <h3>{{ entry.title }}</h3>
            </div>
            <div class="next-icon">
                <i class="fas fa-chevron-right"></i>
            </div>
        </div>
    </a>

{% endfor %}

            
{% if entries %}

<div class="load-button-wrap" s-target="this" s-swap="outerHTML">
    <input type="hidden" name="offset" value="{{ offset + limit }}">
    <button class="load-button" sprig>Load More</button>
</div>

{% endif %}

1 Answer 1

2

Try this:

{{ sprig('_layouts/_categories/_loadMore', {limit: 6, category: category}) }}

{{ sprig.script }}

After query category is not defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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