【发布时间】:2019-11-15 02:58:40
【问题描述】:
我的主题的 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 %}
循环内容截图:
我不确定为什么循环会在内容上循环四次。任何帮助是极大的赞赏。干杯。
【问题讨论】:
-
您是如何验证这种行为的?也许从一开始就是错误的集合。你可以用
{{ stories | length }}来验证物品的数量 -
你使用的是 wood starter 主题吗?
-
@DarkBee 截图显示了 WordPress/Timber/Twig 如何渲染和输出 HTML。
{{ stories | length }}返回 4,但在渲染页面上显示四次。看起来 WordPress 正在选择四个帖子,但 Timber/Twig 正在四个不同的时间循环浏览这些帖子。 -
@Jainil 是的,我从 Timber 的入门主题开始,并一直根据客户的需求对其进行定制。
-
这就是你使用的所有
twig?
标签: php wordpress twig wordpress-theming timber