简体   繁体   English

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

[英]WordPress Timber getting posts of a custom post type

I'm just digging into WordPress plus Timber and I came upon a problem that I can't resolve. 我只是在挖掘WordPress和Timber,我遇到了一个我无法解决的问题。

I have created a custom post type called "project", within which I created a custom field called "project_category". 我创建了一个名为“project”的自定义帖子类型,在其中我创建了一个名为“project_category”的自定义字段。 That custom field contains a checkbox of two choices (graphic, web design). 该自定义字段包含两个选项的复选框(图形,网页设计)。

The question is what can I do to display all the projects that contains the project_category "graphic"? 问题是如何显示包含project_category“graphic”的所有项目?

Here is how I started: 这是我开始的方式:

graphic.php template graphic.php模板

I created a graphic.php file with those wp queries: 我用这些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 Then I create a twig file with this loop. graphic.twig然后我用这个循环创建一个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 %}

With this solution I can get only one project. 有了这个解决方案,我只能获得一个项目。 When I want to display more than one project the project doesn't show up. 当我想显示多个项目时,项目不会显示。 I tried to use "for post in projects" or "for post in post.projects", but nothing worked out really. 我尝试使用“for post in projects”或“post in post.projects”,但没有任何结果。

What can I do to display all the projects that contains the project_category "graphic"? 如何显示包含project_category“graphic”的所有项目?

@filnug, you're almost there. @filnug,你快到了。 I think there's just some confusion about sending vars from PHP to Twig: 我认为将vars从PHP发送到Twig只有一些困惑:

graphic.php: 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 );

twig file: 树枝文件:

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

{% endfor %}

best of luck! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM