使用Timber显示子类别

时间:2018-08-06 13:02:23

标签: php wordpress children timber wp-list-categories

我正在使用以下内容显示顶级类别:

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

但是我需要在一个下拉菜单(引导程序4)中显示子类别。但是我不确定如何访问它们?我想他们已经可以使用cat.child之类的东西了?

1 个答案:

答案 0 :(得分:0)

好吧,这比我想象的要容易得多, 您(和我)完全以错误的方式这样做。 首先,您必须转到主题,然后选择菜单。 那么您必须使所有子类别都属于您希望他们拥有的类别的子代。

完成此操作后,只需输入以下代码即可使用下拉菜单。

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

对我来说这至少有效

相关问题