来自特定 WordPress 类别的帖子未显示在 Timber/Twig for 循环中
Posts from specific WordPress category not displaying in Timber/Twig for loop
我正在使用以下代码输出三个不同类别的帖子。来自加拿大和世界的帖子显示正确,但没有来自美国的帖子。我已经尝试将 var_dump($context['posts']);
转储到 WordPress PHP 文件中,该文件会 return 所有类别的帖子,包括美国。当我在Twig模板中使用Timber的{{ dump(post) }}
转储{{ dump(posts.united-states) }}
时,显示如下int(0)
。我已尝试从类别 slug 中删除连字符并更新 for loop
,但这并没有解决问题。
关于导致此问题的原因有什么想法吗?
PHP代码
$context = Timber::context();
$timber_post = new Timber\Post();
$query = array(
// Canada, United States, and World categories
'cat' => '3,4,6',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
);
$posts = Timber::get_posts( $query );
$sorted_posts = array();
foreach ( $posts as $post ) {
// Get first category of post
$category = $post->category();
// Fill post back to sorted_posts
$sorted_posts[ $category->slug ][] = $post;
}
// Add sorted posts to context
$context['posts'] = $sorted_posts;
$context['post'] = $timber_post;
Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
Twig 模板代码
{% extends "base.twig" %}
{% block content %}
<section>
<h2>Canada</h2>
{% for story in posts.canada %}
<article class="story" id="story-{{ story.ID }}">
</article>
{% endfor %}
</section>
<section>
<h2>United States</h2>
{{ dump(posts.united-states) }}
{% for story in posts.united-states %}
<article class="story" id="story-{{ story.ID }}">
</article>
{% endfor %}
</section>
<section>
<h2>The World</h2>
{% for story in posts.world %}
<article class="story" id="story-{{ story.ID }}">
</article>
{% endfor %}
</section>
{% endblock %}
您是否尝试过像下面这样使用旧方括号访问它:
{{ dump(posts['united-states']) }}
简单示例:https://twigfiddle.com/pbqq14
循环示例:
{% for story in posts['united-states'] %}
{{story}}
{% endfor %}
我正在使用以下代码输出三个不同类别的帖子。来自加拿大和世界的帖子显示正确,但没有来自美国的帖子。我已经尝试将 var_dump($context['posts']);
转储到 WordPress PHP 文件中,该文件会 return 所有类别的帖子,包括美国。当我在Twig模板中使用Timber的{{ dump(post) }}
转储{{ dump(posts.united-states) }}
时,显示如下int(0)
。我已尝试从类别 slug 中删除连字符并更新 for loop
,但这并没有解决问题。
关于导致此问题的原因有什么想法吗?
PHP代码
$context = Timber::context();
$timber_post = new Timber\Post();
$query = array(
// Canada, United States, and World categories
'cat' => '3,4,6',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
);
$posts = Timber::get_posts( $query );
$sorted_posts = array();
foreach ( $posts as $post ) {
// Get first category of post
$category = $post->category();
// Fill post back to sorted_posts
$sorted_posts[ $category->slug ][] = $post;
}
// Add sorted posts to context
$context['posts'] = $sorted_posts;
$context['post'] = $timber_post;
Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
Twig 模板代码
{% extends "base.twig" %}
{% block content %}
<section>
<h2>Canada</h2>
{% for story in posts.canada %}
<article class="story" id="story-{{ story.ID }}">
</article>
{% endfor %}
</section>
<section>
<h2>United States</h2>
{{ dump(posts.united-states) }}
{% for story in posts.united-states %}
<article class="story" id="story-{{ story.ID }}">
</article>
{% endfor %}
</section>
<section>
<h2>The World</h2>
{% for story in posts.world %}
<article class="story" id="story-{{ story.ID }}">
</article>
{% endfor %}
</section>
{% endblock %}
您是否尝试过像下面这样使用旧方括号访问它:
{{ dump(posts['united-states']) }}
简单示例:https://twigfiddle.com/pbqq14
循环示例:
{% for story in posts['united-states'] %}
{{story}}
{% endfor %}