0

我已经设置了一个名为“tours”的自定义帖子类型,其分类法“tour_category”仅特定于该帖子类型。在分类中,我设置了 4 个区域 Golf Breaks、Walking Tours 等......我想要实现的是将 Golf Breaks Taxomony 中的所有帖子渲染到 golf.twig 模板中,但我似乎得到的只是一个页面发现错误。有人可以用这个例子指出我正确的方向吗?

$context = Timber::get_context();
$args = array(
// Get post type tours
    'post_type' => 'tours',
// Get all posts
    'posts_per_page' => -1,
// get post in "Golf Breaks" category
    'meta_query' => array(
        array(
            'key' => 'tour_category',
            'value' => 'Golf Breaks',
            'compare' => 'LIKE'
        )
    ),
// Order by post date
    'orderby' => array(
        'date' => 'DESC'
    ));

$context['Golf Breaks'] = Timber::get_posts( $args );

Timber::render( 'golf.twig', $context );
4

1 回答 1

1

@richard:你会在这条线上度过一段非常糟糕的时光......

$context['Golf Breaks'] = Timber::get_posts( $args );

由于 Twig 使用了数组的属性,因此您需要没有空格(可能是小写字母)的东西。尝试....

$context['golf_breaks'] = Timber::get_posts( $args );

从那里你可以访问 Twig 中的数据...

{% for post in golf_breaks %}
    <h1>{{ post.title }}</h1>
    <div>{{ post.content }}</div>
{% endfor %}

如果您没有看到任何内容,则问题可能与从 WP 获取帖子有关。分类查询可能很困难,尝试仅使用 a 进行调试get_posts以确保 WordPress 找到与您的查询匹配的帖子

于 2017-01-31T15:30:37.597 回答