从 postlist 数组 october cms 获取博客 post?

Get blog post from postlist array october cms?

我正在创建一个博客页面,post列表页面是不同大小图像的形式。我需要能够单独设置每个 post 列表项的样式,因此需要能够使用 twig 访问 post 列表数组并获取 posts。

因此,例如,当从 post 访问特色图片时,您可以使用:

    post.featured_images[0].path

我想这样做,但是 select post 列表中的第一个 post。

无论您想对第一个 post 做什么,您都可以使用循环中的迭代变量访问第一个 post。

Twig中迭代变量很少,我一般用loop.index变量。 例如:

{% for post in posts %}

    {% if loop.index == 1 %}
        {{ post.title }}
        {# this is the first post title #}
    {% else %}
        {{ post.title }}
        {# this is others posts title #}
    {% endif %}

{% endfor %}

然后如果 loop.index == 2 就可以访问第二个 post。如果它等于 3 你可以访问第三个 post 等等

另一种选择是 loop.first

{% if loop.first %}
{# It goes here if it's the first record of the loop #}
{% endif %}
{% if loop.last %}
{# It goes here if it's the last record of the loop #}
{% endif %}

要了解有关 Twig 循环变量的更多信息:http://twig.sensiolabs.org/doc/2.x/tags/for.html#the-loop-variable