2

我正在开发 Locomotive CMS 网站,并正在寻找一种按类别动态列出帖子的方法。

例如我有两个数据模型:帖子和类别

Posts 数据模型有一个与 Category 关联的belong_to 属性。我也有一个类别的模板化视图。

我想做类似的事情:

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

在模板化类别页面上,但到目前为止它没有返回任何结果,即使有这些类别的帖子。

有任何想法吗?谢谢!

4

1 回答 1

0

在试图解决这个问题的过程中,我也发现了这个悬而未决的问题。

这是一个解决方案:

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

这将适用于标准页面。我预计模板化页面可能会出现更多问题,但我也需要实现这一点,因此当/如果我找到解决方案时会更新我的答案。

于 2020-11-11T22:55:41.043 回答