[英]Locomotive CMS: Dynamic with_scope filter on content_type_template page?
I'm working on a Locomotive CMS site and am looking for a way to list posts by categories dynamically.我正在一个 Locomotive CMS 网站上工作,并且正在寻找一种按类别动态列出帖子的方法。
For example I have two data models: Posts and Category例如我有两个数据模型:帖子和类别
The Posts data model has a belong_to attribute associated with Category. Posts 数据模型有一个与类别关联的belong_to 属性。 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!
谢谢!
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.
我预计模板化页面可能会有更多问题,但我也需要实现它,因此当/如果我找到解决方案时会更新我的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.