我使用以下内容显示顶级类别:
$context['categories'] = Timber::get_terms('category');
$categories = get_categories( array(
'hide_empty' => '0',
'exclude' => '1',
'orderby' => 'id',
'order' => 'ASC'
) );
$context['categories'] = $categories;
{% for cat in categories %}
<li {% if term.slug == cat.slug %}class="active"{% endif %}><a href="{{blog.link}}category/{{cat.slug}}">{{cat.name}}</a></li>
{% endfor %}
但我需要在下拉列表中显示子类别(bootstrap 4)。但我不确定如何访问它们?我认为他们已经有像cat.child
这样的东西?
好吧,这比我想象的要容易,你(和我)完全以错误的方式做这件事。首先你必须转到你的主题,然后选择菜单。那么你必须让你所有的子类别都是你希望他们拥有的类别的孩子。
一旦你完成了,你可以输入以下代码,你的下拉菜单就可以了。
{% for item in menu.get_items %}
<li class="nav-item {{ item.classes | join(' ') }}">
{% if item.get_children %}
<div class="dropdown">
<button type="button" class="btn btn-secondary dropdown-toggle" id='dropdownMenuButton' data-toggle="dropdown" aria-haspopup="true" aria-expended="false" >
{{ item.title }}
</button>
<div class="dropdown-menu" aria-labelledby='dropdownMenuButton'>
{% for child in item.get_children %}
<a class="dropdown-item" href="{{child.link}}">{{child.title}}</a>
{% endfor %}
</div>
</div>
{% else %}
<a href="{{ item.link }}" title="{{ item.title }}" class="nav-link">{{ item.title }}</a>
{% endif %}
</li>
{% endfor %}
对我来说这至少起作用了