TransWikia.com

How to use categories with Sprig's "Load More" recipe

Craft CMS Asked by Justin Kendall on September 4, 2021

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

One Answer

Try this:

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

{{ sprig.script }}

After query category is not defined.

Correct answer by Pavel Pavlovich on September 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP