[英]Display all posts from parent category and group by child category with headings in Timber/Twig
[英]TIMBER. Display posts by click the category
我使用此代码显示帖子。 现在我想通过单击网格下方的类别名称来显示帖子。 我该怎么做?
<section class="page-section page-section--padding-2rem">
<div class="row grid-posts">
{% for post in posts %}
{% include 'partial/single-cards/recipes.twig' with {'post': post} %}
{% endfor %}
</div>
在 .php 模板文件中,将类别添加到视图中:
$context['categories'] = Timber::get_terms('category');
在.twig模板中,渲染类别下拉输入。
{% if categories %}
<form action="{{ site.url }}" method="get">
<select name="cat" id="cat" onchange="return this.form.submit()">
{% for cat in categories %}
<option value="{{ cat.id }}">
{{ cat.name }}
</option>
{% endfor %}
<option value="0">Show All</option>
</select>
</form>
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.