Locomotive CMS: Dynamic with_scope filter on content_type_template page?

252 Views Asked by At

I’m working on a Locomotive CMS site and am looking for a way to list posts by categories dynamically.

For example I have two data models: Posts and Category

The Posts data model has a belong_to attribute associated with Category. I also have a templatized view of categories.

I'd like to do something like:

{% with_scope category: category._slug %}
    {% for post in contents.post %}
        {{post.title}}
    {% endfor %}
{% endwith_scope %}

on the templatized category page, but so far it doesn't return any results, even though there are posts with those categories.

Any ideas? Thank you!

1

There are 1 best solutions below

0
On

I found this unanswered question in the process of trying to figure this out as well.

Here is a solution:

// Iterate through all categories
{% for category in contents.categories %}

    // Assign the current iteration category to a variable
    {% assign loopCategory = category %}

    // with_scope the current iteration category
    {% with_scope category: loopCategory %}

        // Check if this category has been assigned to any content entries (posts, or 
        whatever else you might be classifying)
        {% if contents.posts != empty %}

            {% for post in contents.posts %}

                // display your post / content entry info

            {% endfor %}

        {% else %}

        {% endif %}

    {% endwith_scope %}

{% endfor %}

This will work on a standard page. I expect the templatised page may be more problematic, but I need to implement that as well so will update my answer when/if I find a solution.