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?


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


{% 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 class="more-post-content">
            <div class="more-post-title">
                <h3>{{ entry.title }}</h3>
            <div class="next-icon">
                <i class="fas fa-chevron-right"></i>

{% 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>

{% endif %}


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, privacy policy and cookie policy

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