【问题标题】:Get blog post from postlist array october cms?从 postlist 数组 october cms 获取博客文章?
【发布时间】:2017-01-05 18:23:28
【问题描述】:

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

例如,当您从帖子中访问特色图片时,您可以使用:

    post.featured_images[0].path

我想这样做,但要选择帖子列表中的第一个帖子。

【问题讨论】:

  • 嗨,如果您不提供一些代码和示例来说明您想要实现的目标,您的问题将会被否决。这是你的第一篇文章,所以我建议你先编辑他的问题。欢迎来到 Stack Overflow :)
  • 嗨@El_Matella 这更好吗?你知道我该怎么做吗?

标签: laravel twig octobercms


【解决方案1】:

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

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,你可以访问第二个帖子。如果它等于 3,您可以访问第三个帖子等。

另一种选择是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

【讨论】:

  • 谢谢艾哈迈德!这正是我所需要的!
猜你喜欢
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
相关资源
最近更新 更多