WordPress Timber获得自定义帖子类型的帖子


WordPress Timber getting posts of a custom post type

我正在挖掘WordPress + Timber,我遇到了一个我无法解决的问题。

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

问题是我能做些什么来显示包含project_category "graphic"的所有项目?

我是这样开始的:

<<p> graphic.php模板/strong>

我用这些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"。

如何显示包含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 %}
祝你好运!