我在主题的index.php文件中有以下代码。它被设置为从一系列类别ID中抓取四个最近的帖子。ID是父类别和子类别的混合。
$args = array(
'cat' => '7,5,3,4,6',
'numberposts' => 4,
'order' => 'DESC',
);
$context['stories'] = Timber::get_posts($args);tease.twig文件中使用了以下代码。
{% block content %}
{{ post.content }}
{% endblock %}以下代码用于tease-stories.twig文件。
{% extends "tease.twig" %}
{% block content %}
{% for story in stories %}
<article class="story" id="story-{{post.ID}}">
{% if loop.first %}
{% if story.thumbnail.src %}
<img src="{{story.thumbnail.src}}" class="" alt="" />
{% endif %}
{% endif %}
<h3 class="story__heading">
<a href="{{ story.link }}">
{{ story.title }}
</a>
</h3>
<div class="story__meta">
<time class="">{{ story.date }}</time>
</div>
{% if loop.first %}
<div class="story__content">
{{ story.preview.read_more(false) }}
</div>
{% endif %}
</article>
{% endfor %}
{% endblock %}index.twig文件中使用了以下代码。
{% extends "base.twig" %}
{% block content %}
<section class="stories">
<h2>Latest Travel Stories</h2>
{% for story in stories %}
{% include ['tease-stories.twig'] %}
{% endfor %}
</section>
<section class="observations">
<h2>Observations</h2>
{% for observation in observations %}
{% include ['tease-observations.twig'] %}
{% endfor %}
<a href="{{ site.url }}/gerry/observations" title="More observations" class="more more-observations">
More Observations
</a>
</section>
{% endblock %}循环内容的截图:

我不确定为什么循环会在内容上循环四次。任何帮助都是非常感谢的。干杯。
发布于 2019-11-17 01:40:37
我已经在@DarkBee的帮助下解决了这个问题。问题是使用了两次for loop,一次在index.twig文件中,一次在tease.twig文件中,这导致故事被多次输出。我更新的index.twig代码如下。
{% extends "base.twig" %}
{% block content %}
<section class="stories">
<h2>Latest Travel Stories</h2>
{% include ['tease-stories.twig'] %}
</section>
<section class="observations">
<h2>Observations</h2>
{% include ['tease-observations.twig'] %}
<a href="{{ site.url }}/gerry/observations" title="More observations" class="more more-observations">
More Observations
</a>
</section>
{% endblock %}https://stackoverflow.com/questions/58869710
复制相似问题