WordPress Timber 获得 post 的自定义 post 类型

WordPress Timber getting posts of a custom post type

我正在深入研究 WordPress 和 Timber,我遇到了一个我无法解决的问题。

我创建了一个名为 "project" 的自定义 post 类型,我在其中创建了一个名为 "project_category" 的自定义字段。该自定义字段包含两个选项(图形、网页设计)的复选框。

问题是如何显示包含 project_category "graphic" 的所有项目?

我是这样开始的:

graphic.php模板

我用这些 wp 查询创建了一个 graphic.php 文件:

$context = Timber::get_context();

$args = array(
    // Get post type project
    'post_type' => 'project',
    // Get all posts
    'posts_per_page' => -1,
    // Gest post by "graphic" category
    'meta_query' => array(
        array(
            'key' => 'project_category',
            'value' => 'graphic',
            'compare' => 'LIKE'
        )
    ),
    // Order by post date
    'orderby' => array(
        'date' => 'DESC'
    ),
);

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

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

graphic.twig 然后我用这个循环创建一个树枝文件。

{% extends "base.twig" %}

{% block content %}

<div class="l-container">

    <main role="main">
        <div class="l-row">
            <h1>My graphic design projects</h1>

            {% for post in posts %}

                <a href="{{ post.link }}" class="project-images l-col l-col--1-of-4 l-col--m-1-of-2">
                    <h2>{{ post.title }}</h2>

                        {% if post.thumbnail %}
                            <img src="{{post.get_thumbnail.src('medium_large')}}" alt="{{post.title}}" />
                        {% endif %}
                </a>

            {% endfor %}
        </div> 
    </main>

</div>

{% endblock %}

使用此解决方案,我只能获得一个项目。当我想显示多个项目时,该项目不显示。 我尝试使用 "for post in projects" 或 "for post in post.projects",但没有任何效果。

如何显示包含 project_category "graphic" 的所有项目?

@filnug,你快到了。我认为将变量从 PHP 发送到 Twig:

只是有些混乱

graphic.php:

$context = Timber::get_context();
$args = array(
// Get post type project
'post_type' => 'project',
// Get all posts
'posts_per_page' => -1,
// Gest post by "graphic" category
'meta_query' => array(
    array(
        'key' => 'project_category',
        'value' => 'graphic',
        'compare' => 'LIKE'
    )
),
// Order by post date
'orderby' => array(
    'date' => 'DESC'
));

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

树枝文件:

{% for post in graphics %}
    <h2>{{ post.title }}</h2>
    (other markup goes here)

{% endfor %}

祝你好运!