donglu9743 2016-10-25 12:19
浏览 27
已采纳

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

I'm just digging into WordPress plus Timber and I came upon a problem that I can't resolve.

I have created a custom post type called "project", within which I created a custom field called "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"?

Here is how I started:

graphic.php template

I created a graphic.php file with those wp queries:

$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.

{% 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.

What can I do to display all the projects that contains the project_category "graphic"?

  • 写回答

1条回答 默认 最新

  • doudao7511 2016-11-01 13:14
    关注

    @filnug, you're almost there. I think there's just some confusion about sending vars from PHP to 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 );
    

    twig file:

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

    best of luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c