1

目前我列出了所有帖子类别,但我希望排除某些存在的类别,例如未分类。

这就是我目前启动类别的方式:

$context['categories'] = Timber::get_terms('category');

并通过列出类别

{% for cat in categories %}

<li><a style="margin: 0;" href="{{cat.link}}">{{cat.name}}</a></li>

{% endfor %}
4

2 回答 2

1

你有几种可能性。这里有两个:

1. 查询中排除

使用时Timber::get_terms()Timber::get_posts()您可以使用与WordPressget_terms()功能相同的参数。

exclude参数需要您要排除的术语 ID。未分类通常具有 ID 1。

$context['categories'] = Timber::get_terms( 'category', array(
    'exclude' => 1
) );

2. 在 Twig 中排除

Twig 允许您向for 循环添加条件以排除您想要在循环中忽略的项目。

这样,您可以例如检查 slug 是否“未分类”,但您也可以使用category.id != 1.

{% for cat in categories if category.slug != 'uncategorized' %}
    <li><a href="{{ cat.link }}">{{ cat.name }}</a></li>
{% endfor %}
于 2016-09-20T18:56:47.527 回答
0

如果您知道不想包含的类别的名称,那么您可以在下面的代码中使用not 。

{% set categorisedValues = ['one','two','three','four','five'] %}
{% set uncategorisedValues = ['one','two'] %}

{% for categorised in categorisedValues %}
    {% if categorised not in uncategorisedValues %}
        {{ categorised  }}
    {% endif %}
{% endfor %}
于 2016-09-21T08:26:17.773 回答